我在同一个 Nginx 服务器上有 2 个站点。
我的第一个网站https://www.example.com以下所有地址均必须重定向到此域:
xx.xx.xx.xxx
我的第二个网站https://analytics.example.com以下地址必须重定向至该域:
如果我禁用第二个站点,第一个站点仍能正常工作。
当我使用 Nginx 激活多个站点时,会发生冲突。我该如何解决?
使用以下设置:
ubuntu@www-example-com ~ $ sudo nginx -t
nginx: [emerg] duplicate listen options for [::]:80 in /etc/nginx/sites-enabled/www-example-com:3
nginx: configuration file /etc/nginx/nginx.conf test failed
www.example.com
server {
listen 80 default_server;
listen [::]:80 ipv6only=on default_server;
server_name example.com www.example.com;
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /var/www/letsencrypt;
}
location / {
return 301 https://www.example.com$request_uri;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2 ipv6only=on;
server_name www.example.com;
root /var/www/www-example-com/web;
index index.php;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
expires 1209600s;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(txt|log)$ {
deny all;
}
location ~ \..*/.*\.php$ {
return 403;
}
location ~ ^/sites/.*/private/ {
return 403;
}
location ~ ^/sites/[^/]+/files/.*\.php$ {
deny all;
}
location ~* ^/.well-known/ {
allow all;
}
location ~ (^|/)\. {
return 403;
}
location / {
try_files $uri /index.php?$query_string;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?q=$1;
}
location ~ /vendor/.*\.php$ {
deny all;
return 404;
}
location ~ '\.php$|^/update.php' {
expires off;
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
include fastcgi_params;
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
location ~ ^/sites/.*/files/styles/ {
try_files $uri @rewrite;
}
location ~ ^(/[a-z\-]+)?/system/files/ {
try_files $uri /index.php?$query_string;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
try_files $uri @rewrite;
expires max;
log_not_found off;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
location / {
return 301 https://www.example.com$request_uri;
}
}
analytics.example.com
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name analytics.example.com;
location / {
return 301 https://analytics.example.com$request_uri;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2 ipv6only=on;
server_name analytics.example.com;
root /var/www/analytics-example-com/web;
index report.html;
auth_basic "Protected";
auth_basic_user_file /var/www/analytics-example-com/web/.htpasswd;
ssl_certificate /etc/letsencrypt/live/analytics.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/analytics.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
对于这两个站点,我遇到了相同的错误:
答案1
ipv6only=on
是多余的,因为这是默认设置。它不需要出现在listen
配置中的任何指令中。
如果存在,它也只能对于任何给定的地址和端口对出现一次。
您应该将其从所有listen
指令中删除。
答案2
错误消息nginx: [emerg] duplicate listen options for...
意味着您对同一个端口多次应用了同一个参数。也就是说,ipv6only=on
此处代码中的第二个参数:
示例.com:
listen [::]:80 ipv6only=on default_server;
analytics.example.com
listen [::]:80 ipv6only=on; # <- this is duplicate and raises the error
线索就隐藏在nginx 文档:
listen 指令可以包含多个特定于套接字相关系统调用的附加参数。这些参数可以在任何 listen 指令中指定,但对于给定的地址:端口对只能指定一次。
这适用于任何“附加”参数(例如,,,deferred
等)。当然,正如前面提到的,您不再需要指定任何内容。reuseport
so_keepalive
ipv6only=on
那么最好的做法是仅在第一个或 default_server 声明中指定任何参数。可以假设后续listen
指令将继承这些。