【问题标题】:Apache and ultimate config for nginx to serve all virtual hosts in the right wayApache 和 nginx 的终极配置,以正确的方式为所有虚拟主机提供服务
【发布时间】:2011-03-26 22:32:16
【问题描述】:

我刚刚将 nginx 设置为在一个站点上提供静态请求,但是我的服务器上有很多站点,我想知道是否应该为所有站点正确配置新的 nginx 服务器? 我现在在做什么。我有 Apache 的所有虚拟主机条目的文件,如下所示:

NameVirtualHost *:8080
<VirtualHost *:8080>
 ServerName sky2high.net
 DocumentRoot /home/mainsiter/data/www/sky2high.net
</VirtualHost>

<VirtualHost *:8080>
 ServerName surdo.asmon.ru
 DocumentRoot /home/surdo/data/www/surdo.asmon.ru
</VirtualHost>

<VirtualHost *:8080>
 ServerName surdoserver.ru
 DocumentRoot /home/surdo/data/www/surdoserver.ru
</VirtualHost>

我在 apache 的 ports.conf 中有这个:

Listen 8080

所以我设置了 nginx 以使用一个站点 (sky2high.net),创建下一个配置文件 (/etc/nginx/sites-enabled/sky2high.net):

server {
 listen 80;
 server_name sky2high.net www.sky2high.net;
  proxy_pass http://127.0.0.1:8000;
   proxy_set_header Host $host;

 access_log /var/log/nginx.access_log;

 location ~* \.(jpg|jpeg|gif|png|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|tar|wav|bmp|rtf|swf|ico|flv|txt|xml|docx|xlsx)$ {
  root /home/mainsiter/data/www/sky2high.net/;
  index index.php;
  access_log off;
  expires 30d;
 }
 location ~ /\.ht {
  deny all;
 }
 location / {
  proxy_pass http://127.0.0.1:8080/;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-for $remote_addr;
  proxy_set_header Host $host;
  proxy_connect_timeout 60;
  proxy_send_timeout 90;
  proxy_read_timeout 90;
  proxy_redirect off;
  proxy_set_header Connection close;
  proxy_pass_header Content-Type;
  proxy_pass_header Content-Disposition;
  proxy_pass_header Content-Length;
 }
} 

它在这个域上工作得很好,但当然另一个虚拟主机坏了。

所以,问题是:nginx 是否有终极配置选项,女巫可以帮助处理来自所有虚拟主机(域)的所有请求并以正确的方式为它们提供服务?我的意思是,允许不为每个虚拟主机编写单独的配置文件的选项(所有这些加倍的东西,如根和索引选项),但所有虚拟主机只有一个?

PS:我应该将问题移至 serverfault 吗?

更新: 嗯..我想知道它是如何工作的,但确实如此。我已经制作了下一个配置文件:

/etc/nginx/nginx.conf

user www-data;
worker_processes  2;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip             on;   
    gzip_min_length  1000;
    gzip_proxied     any;
    gzip_disable     "msie6";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-enabled/默认

server {
    listen 80;

    location / {
        proxy_pass http://127.0.0.1:8080/;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Connection close;
        proxy_pass_header Content-Type;
        proxy_pass_header Content-Disposition;
        proxy_pass_header Content-Length;
    }
}

我不明白它是如何工作的,但它是......

更新 2:或者它不起作用!我在控制台中查看了“顶部”并提到 apache 不仅服务于 php 请求,还服务于静态内容 =(

【问题讨论】:

    标签: apache nginx virtual-hosts


    【解决方案1】:

    您现在所做的是将所有网络流量发送到 127.0.0.1:8080,而不允许 Nginx 提供静态文件。

    您应该尝试以下方法:

    server {
    listen 80;
    server_name sky2high.net www.sky2high.net;
    location / {
    proxy_pass http://127.0.0.1:8080;
    include /etc/nginx/conf.d/proxy.conf;
    }
    location ~* ^.+\.(jpg|jpeg|gif|png|ico|tgz|gz|pdf|rar|bz2|exe|ppt|txt|tar|mid|midi|wav|bmp|rtf) {
    root /folder/to/static/files;
    expires 90d;
    }
    location ~* ^.+\.(css|js)$ {
    root /folder/to/static/files;
    expires 30d;
    }
    

    然后在 proxy.conf 中输入以下内容:

    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size 8m;
    client_body_buffer_size 256k;
    proxy_connect_timeout 60;
    proxy_send_timeout 60;
    proxy_read_timeout 60;
    proxy_buffer_size 4k;
    proxy_buffers 32 256k;
    proxy_busy_buffers_size 512k;
    proxy_temp_file_write_size 256k;
    

    这应该适合你

    【讨论】:

    • 您可能希望将这一行更改为:proxy_set_header Host $host:$proxy_port;
    【解决方案2】:

    就我的两分钱,在大多数情况下,没有必要指定listen 80

    来源:Nginx common Pitfalls

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 2013-08-22
      • 2011-05-04
      • 2017-06-14
      • 2012-08-22
      • 2011-10-13
      相关资源
      最近更新 更多