我有以下设置:
- 具有两个虚拟主机的 Nginx
- 两个子域名:sub1.domain.com 和 sub2.domain.com
sub1.domain.com
运行正常。如果我输入浏览器,则返回 403。我已将两个域的配置目录设置为归( )sub2.domain.com
所有。www-data
chown -R www-data:www-data /var/www/
查看错误日志可发现为/var/log/nginx/sub2.domain.com/error.log
空,而/var/log/nginx/sub1.domain.com/error.log
包含
2018/03/09 11:29:00 [error] 19024#0: *13009 directory index of "/var/www/sub1.domain.com" is forbidden, client: ip, server: sub1.domain.com, request: "GET / HTTP/1.1", host: "my-host"
sub1.domain.com
有谁知道为什么即使我已经sub2.domain.com
在浏览器中输入,服务器仍然包含它?!
我认为问题可能是,似乎sub2.domain.com
总是执行的配置sub1.domain.com
(即default_server
)。根据 nginx 文档:
如果它的值与任何服务器名称都不匹配,或者请求根本不包含此标头字段,那么 nginx 将会把请求路由到此端口的默认服务器。
sub1.domain.com 的 Nginx 配置
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/sub1.domain.com;
index index.php index.html index.htm;
server_name sub1.domain.com;
access_log /var/log/nginx/sub1.domain.com/access.log;
error_log /var/log/nginx/sub1.domain.com/error.log;
location / {
try_files $uri $uri/ =404;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
location ~ /\.ht {
deny all;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param AUTH_SECRET "my-secret";
include fastcgi_params;
}
}
sub2.domain.com 的 Nginx 配置
upstream backend{
server 127.0.0.1:3000;
}
server {
listen 80;
listen [::]:80;
access_log /var/log/nginx/sub2.domain.com/access.log;
error_log /var/log/nginx/sub2.domain.com/error.log;
root /var/www/sub2.domain.com;
server_name sub2.domain.com;
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
location ~ /\.ht {
deny all;
}
# route /api requests to backend
location /api/v1 {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
}
location / {
try_files $uri $uri/ /index.html =404;
}
}
我已经按照这里的教程https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts并设置符号链接。因此/etc/nginx/sites-available
包含配置并/etc/nginx/sites-enabled
包含这些配置上的符号链接。
还要注意,它sub1.domain.com
提供 php 后端,而sub2.domain.com
应该提供一些静态内容(单页应用程序)并将 api 请求定向到 Node.js 后端。
编辑:
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
我的nginx.conf
文件包含include /etc/nginx/sites-enabled/*;
。因此,我上面发布的配置应该包括在内。nginx -t
如果其中一个配置有错误,则至少会抛出错误。
编辑 2:tl;dr(评论摘要):DNS 配置错误。A
为两个子域设置正确的记录解决了该问题。nginx 配置按预期运行。