我正在尝试使用 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;
}
答案1
您混淆了您的listen
和server_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;
答案2
更新配置文件后,我运行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;
(...)
}
现在一切都运行良好。谢谢大家!