2014-12-13 5 views
1

내 서버에 여러 계정/도메인이 있습니다. 나 아파치 2.4와 cPanel을 사용하고 프론트 리버스 프록시로 Nginx를 사용하고 싶었어요. 내가 아파치 포트를 변경 Nginx를 설치하고 그것은 잘 작동 하나의 도메인/계정. 나는 서버의 모든 도메인과 앞으로의 모든 계정에 사용하고 싶습니다. 특정 도메인 대신 변수 $domain을 입력하려고했지만 나중에 nginx가 변수를 지원하지 않는다는 것을 깨달았습니다. 사용자 디렉토리의 경우와 동일합니다. 내가 할 노력하고있어여러 도메인에 대한 Nginx 역방향 프록시 구성

user nobody; 
worker_processes 4; 
error_log logs/error.log crit; 

worker_rlimit_nofile 8192; 

events { 
worker_connections 1024; # you might need to increase this setting for busy servers 
use epoll; # Linux kernels 2.6.x change to epoll 
} 

http { 
server_names_hash_max_size 2048; 
server_names_hash_bucket_size 512; 

server_tokens off; 

include mime.types; 
default_type application/octet-stream; 

sendfile on; 
tcp_nopush on; 
tcp_nodelay on; 
keepalive_timeout 10; 

# Gzip on 
gzip on; 
gzip_min_length 1100; 
gzip_buffers 4 32k; 
gzip_types text/plain application/x-javascript text/xml text/css; 

# Other configurations 
ignore_invalid_headers on; 
client_max_body_size 8m; 
client_header_timeout 3m; 
client_body_timeout 3m; 
send_timeout  3m; 
connection_pool_size 256; 
client_header_buffer_size 4k; 
large_client_header_buffers 4 32k; 
request_pool_size 4k; 
output_buffers 4 32k; 
postpone_output 1460; 

# Cache most accessed static files 
open_file_cache   max=10000 inactive=10m; 
open_file_cache_valid 2m; 
open_file_cache_min_uses 1; 
open_file_cache_errors on; 

# virtual hosts includes 
include "/etc/nginx/conf.d/*.conf"; 

server { 
    # this is your access logs location 
    access_log /usr/local/apache/domlogs/accountusername/example.com; 

    error_log logs/vhost-error_log warn; 
    listen 80; 
    # change to your domain 
    server_name example.com www.example.com; 

    location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css)$ { 
    # this is your public_html directory 
    root /home/accountusername/public_html; 
} 
location/{ 
    client_max_body_size 10m; 
    client_body_buffer_size 128k; 

    proxy_send_timeout 90; 
    proxy_read_timeout 90; 

    proxy_buffer_size 4k; 
    proxy_buffers  16 32k; 
    proxy_busy_buffers_size 64k; 
    proxy_temp_file_write_size 64k; 

    proxy_connect_timeout 30s; 

    # change to your domain name 
    proxy_redirect http://www.example.com:8080 http://www.example.com; 
    proxy_redirect http://example.com:8080 http://example.com; 

    proxy_pass http://127.0.0.1:8080/; 
    proxy_set_header Host $host; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    } 
} 
} 

는 서버에있는 모든 도메인과 추가됩니다 향후 도메인에 대해 작동하는 코드를 삽입하는 것입니다 : 여기 내 설정 파일입니다. 일부 포럼과 블로그에서 가상 호스트 설정 (서버 블록)에 대해 설명하지만 실제로 어떤 용도로 사용되는지는 잘 모르겠습니다. 만약 누군가가 이것에 대해 어떤 정보라도 제공한다면 나는 그것을 고맙게 생각할 것입니다. 가상 호스트를 설정해야합니까? 구성 파일에서 무엇을 변경해야합니까? 고맙습니다.

답변

2

당신의 설정은

server { 
    listen frontip:80 default_server; 

    location/{ 
     proxy_pass http://127.0.0.1:8080; 
     proxy_set_header Host $host; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_redirect http://$host:8000/ http://$host/; 
    } 
} 

거의 정확하지만, 당신에게 가장 좋은 방법은 8080 포트를 사용하지 마십시오. 당신이 필요로하는 것은 nginx에게 오직 외부 IP만을 바인드하라는 것입니다. 각 listenbind이라는 키워드를 각각 server에 추가하십시오. 당신은 아무것도 그리워하지 않는 경우

server { 
    listen frontip:80 default_server bind; 

    location/{ 
     proxy_pass http://127.0.0.1; 
     proxy_set_header Host $host; 
     proxy_set_header X-Real-IP $remote_addr; 
    } 
} 

,의 nginx는 127.0.0.1:80 결합하지 않을 것이다, 그래서 아파치는 바인딩 할 수 있습니다.

리디렉션 다시 쓰기가 필요 없기 때문에이 경우 proxy_redirect 지시어가 필요하지 않습니다.

루트 폴더의 경우 변수를 사용할 수 있지만 훨씬 더 유용합니다. map; 지도에 대한

http { 
    ... 
    map $host $root { 
     hostnames; 
     default /var/www; 
     .domain1.com /home/user1/domain1.com; 
     custom.domain1.com /home/user1/custom; 
     domain2.com /home/user2/domain2.com; 
     www.domain2.com /home/user2/domain2.com; 
    } 

    server { 
     listen frontip:80 default_server; 
     root $root; 

     location/{ 
      proxy_pass http://127.0.0.1; 
      proxy_set_header Host $host; 
      proxy_set_header X-Real-IP $remote_addr; 
     } 

     location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css)$ { 
     } 
    } 
} 

http://nginx.org/en/docs/http/ngx_http_map_module.html

+0

매우 도움이 파비콘이 표시되지 않습니다 왜 그렇게 : 당신은 알고 계십니까 감사? 코드 행이 누락 된 것일까 요? –

+0

**이 코드는 ** server {} **'location =/favicon.ico {log_not_found off; access_log off;}'에서 사용했지만 작동하지 않았습니다. –

+0

올바른 위치입니다. 'log_not_found off'를 제거하고 nginx 파일을 찾을 수없는 error_log를 확인하십시오. –

0

아이디어는 환상적입니다. 좋고 예측 가능한 방식으로 작동하려면, 여러분이 서비스하는 모든 서버에 대해 "서버"블록을 생성해야하며 그에 따라 도메인 이름을 "proxy_redirect"지시어에 맞게 작성해야합니다.

많은 도메인을 처리하려면 - 그 목록을 얻고 실제 설정을 생성하기 위해 shell \ perl \ python 스크립트를 작성하십시오. 이 스크립트는 다소 단순합니다.

그리고 "서버 블록"이 무엇인지 명확히 이해하려면 문서를 읽으십시오. 곧, 그들은 nginx의 성능 마법의 핵심입니다.

관련 문제