简体   繁体   English

用于多个域的Nginx反向代理配置

[英]Nginx reverse proxy configuration for multiple domains

I have multiple accounts/domains on my server. 我的服务器上有多个帐户/域。 I'm using cPanel with Apache 2.4 and wanted to use Nginx as a front reverse proxy. 我将cPanel与Apache 2.4结合使用,并希望将Nginx用作前端反向代理。 I changed Apache port, installed Nginx and it works fine but for one domain/account only. 我更改了Apache端口,安装了Nginx,它工作正常,但仅适用于一个域/帐户。 I want to use it for all my domains on the server, and any future accounts. 我想将其用于服务器上的所有域以及将来的帐户。 I tried to enter $domain variable instead of a specific domain but realized later that nginx doesn't support variables. 我尝试输入$domain变量而不是特定域,但后来意识到nginx不支持变量。 Same thing with the user directory. 与用户目录相同。 Here is my config file: 这是我的配置文件:

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;
  }
}
}

What I'm trying to do is to place a code that works for all domains on the server and any future domains will be added. 我想做的是在服务器上放置一个适用于所有域的代码,以后将添加任何域。 I see some forums and blogs explain to setup virtual hosts (Server blocks) but I'm not sure what they're used for. 我看到一些论坛和博客解释了如何设置虚拟主机(服务器块),但是我不确定它们的用途。 I'd appreciate it if anyone provide any info about this. 如果有人提供任何有关此信息,我将不胜感激。 Should I setup virtual hosts? 我应该设置虚拟主机吗? What is needed to be changed in my configuration file? 在我的配置文件中需要更改什么? Thank you. 谢谢。

You config is almost correct 您的配置几乎正确

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/;
   }
}

But best way to you do not use 8080 port. 但最好的方法是不要使用8080端口。 All you need is tell to nginx to bind only external ip. 您需要告诉nginx仅绑定外部ip。 Add ip and bind keyword to all your listen in each server . 在每个server所有listen中添加ip和bind关键字。

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;
   }
}

If you missed nothing, nginx will not bind 127.0.0.1:80, so apache can bind it. 如果您什么都没错过,nginx不会绑定127.0.0.1:80,因此apache可以绑定它。

In this case you do not need any proxy_redirect directives because you don't need any redirect rewrites. 在这种情况下,您不需要任何proxy_redirect指令,因为您不需要任何重定向重写。

For root folder you can use variables but much better use map ; 对于根文件夹,可以使用变量,但最好使用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)$ {
       }
    }
}

More about map http://nginx.org/en/docs/http/ngx_http_map_module.html 有关地图的更多信息http://nginx.org/en/docs/http/ngx_http_map_module.html

Your idea is a kind of fantastic. 你的主意真是太棒了。 To operate in good and predictable\\debuggable way, you should create "server" block for every server you serve, and you should write it domain name into "proxy_redirect" directive accordingly. 为了以良好且可预测的\\可调试的方式运行,您应该为所服务的每个服务器创建“服务器”块,并将域名相应地写入“ proxy_redirect”指令中。

To handle a lot of domains - get a list of them and write shell\\perl\\python script to generate your actual config. 要处理许多域,请获取它们的列表并编写shell \\ perl \\ python脚本以生成您的实际配置。 This script will be rather simple one. 该脚本将非常简单。

And read the docs - to understand clearly what "server blocks" are for. 并阅读文档-清楚地了解“服务器块”的用途。 Shortly, they are the core of nginx's performance magic. 不久,它们是Nginx表演魔术的核心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM