【问题标题】:Nginx reverse proxy configuration for multiple domainsNginx 多域反向代理配置
【发布时间】:2015-02-12 03:41:08
【问题描述】:

我的服务器上有多个帐户/域。我将 cPanel 与 Apache 2.4 一起使用,并希望将 Nginx 用作前端反向代理。我更改了 Apache 端口,安装了 Nginx,它工作正常,但仅适用于一个域/帐户。我想将它用于服务器上的所有域以及任何未来的帐户。我尝试输入 $domain 变量而不是特定域,但后来意识到 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;
  }
}
}

我要做的是在服务器上放置一个适用于所有域的代码,并且将添加任何未来的域。我看到一些论坛和博客解释设置虚拟主机(服务器块),但我不确定它们的用途。如果有人提供有关此的任何信息,我将不胜感激。我应该设置虚拟主机吗?我的配置文件中需要更改什么?谢谢。

【问题讨论】:

    标签: apache nginx webserver cpanel reverse-proxy


    【解决方案1】:

    你的配置几乎是正确的

    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

    【讨论】:

    • 非常有帮助,非常感谢 :) 你知道为什么网站图标不显示吗?可能少了一行代码?
    • 我在 server { } location = /favicon.ico {log_not_found off;access_log off;} 中尝试了这段代码,但没有成功。
    • 位置正确。删除 log_not_found off 并检查 error_log 中哪个文件 nginx 未找到。
    • 太棒了!谢谢你:)
    【解决方案2】:

    你的想法有点奇妙。为了以良好且可预测\可调试的方式运行,您应该为您服务的每个服务器创建“服务器”块,并相应地将其域名写入“proxy_redirect”指令。

    要处理大量域 - 获取它们的列表并编写 shell\perl\python 脚本来生成您的实际配置。这个脚本会很简单。

    并阅读文档 - 清楚地了解“服务器块”的用途。很快,它们就是 nginx 性能魔法的核心。

    【讨论】: