多个域的Nginx反向代理配置

时间:2014-12-13 15:23:28

标签: apache nginx webserver cpanel reverse-proxy

我的服务器上有多个帐户/域。我正在使用带有Apache 2.4的cPanel,并希望使用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;
  }
}
}

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

2 个答案:

答案 0 :(得分: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关键字添加到每个listen中的所有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,所以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

的详情

答案 1 :(得分:0)

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

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

阅读文档 - 清楚地了解“服务器块”的用途。不久,它们是nginx表现魔术的核心。

相关问题