Ubuntu

不能在同一個 NGINX 伺服器下執行兩個獨立的站點

  • January 4, 2014

我正在嘗試使用 nginx 在同一台伺服器上執行兩個獨立的站點。根據配置,nginx 在兩個域名下為一個站點或另一個站點提供服務。它不會在自己的域上執行每個站點。

有任何想法嗎?謝謝!

**更新:**我已經嘗試了 Michael Hampton 的建議,但是當有兩個 server_name 指令時伺服器不會啟動。如果我評論其中之一,nginx 會啟動,但只執行一個網站。

service nginx configtest只能使用一個 server_name,使用兩個 server_name 它會失敗。

配置文件如下:

/etc/nginx/sites-available/joomla

server {
   listen 80;
   server_name n-pix.com;

   root /var/www/n-pix;
   index index.php index.html index.htm default.html default.htm;

   error_log    /var/log/nginx/joomla.error.log info;

   # Support Clean (aka Search Engine Friendly) URLs
   location / {
           try_files $uri $uri/ /index.php?$args;
   }

   client_max_body_size 1024M;

   server_tokens off;

   # deny running scripts inside writable directories
   location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
           return 403;
           error_page 403 /403_error.html;
   }

   location ~ \.php$ {
       fastcgi_pass  unix:/var/run/php5-fpm.sock;
       fastcgi_index index.php;
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   }

   # caching of files 
   location ~* \.(ico|pdf|flv)$ {
           expires 1y;
   }

   location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
           expires 14d;
   }

}

/etc/nginx/sites-available/jarbas

upstream unicorn {
 server unix:/tmp/unicorn.jarbas.sock fail_timeout=0;
}

server {
 listen 80;
 server_name jarbas.n-pix.com;

 root /home/deployer/apps/jarbas/current/public;

 error_log    /var/log/nginx/jarbas.error.log info;

 location ^~ /assets/ {
   gzip_static on;
   expires max;
   add_header Cache-Control public;
 }

 try_files $uri/index.html $uri @unicorn;
 location @unicorn {
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header Host $http_host;
   proxy_redirect off;
   proxy_pass http://unicorn;
 }

 error_page 500 502 503 504 /500.html;
 client_max_body_size 1G;
 keepalive_timeout 10;
}

你混淆了你的listenserver_name指令。

listen應該包含您希望伺服器偵聽的埠(以及可選的 IP/IPv6 地址)。

server_name應該包含伺服器的主機名。

例如(此配置需要 nginx 1.3.4 或更高版本):

listen 80;
listen [::]:80;
server_name n-pix.com;

listen 80;
listen [::]:80;
server_name jarbas.n-pix.com;

更新配置文件後,我執行nginx -t

nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
nginx: configuration file /etc/nginx/nginx.conf test failed

所以我添加server_names_hash_bucket_size 64;nginx.conf

http {
   (...)

   server_names_hash_bucket_size 64;

   (...)
}

現在一切正常。多謝你們!

引用自:https://serverfault.com/questions/564977