2014-12-13 51 views
1
反向代理配置

我有我的服務器上有多個帳戶/域。我在Apache 2.4上使用cPanel,並希望使用Nginx作爲前端反向代理。我更改了Apache端口,安裝了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。將ip和bind關鍵字添加到每個server的所有listen中。

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,以便Apache可以綁定。

在這種情況下,你不需要任何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

非常有幫助,非常感謝你:)你知道爲什麼網站圖標顯示不出來?可能缺少一行代碼? – 2014-12-14 15:44:18

+0

我在** server {} **'location = /favicon.ico {log_not_found off; access_log off;}裏面試過這段代碼,但它不起作用。 – 2014-12-14 16:32:41

+0

這是正確的位置。刪除'log_not_found off'並檢查error_log找不到nginx文件。 – 2014-12-14 18:47:22

0

你的想法是一種夢幻般的。要以良好和可預測的\可調試的方式進行操作,您應該爲每個服務器的服務器創建「服務器」塊,並且應該相應地將其域名寫入「proxy_redirect」指令。

處理很多領域的 - 讓他們的列表,並編寫shell \ Perl的\ python腳本來生成實際的配置。這個腳本將是相當簡單的。

和閱讀文檔 - 要了解清楚什麼是「服務器模塊」是。不久,它們成爲nginx性能魔法的核心。