Nginx

如何使用 Nginx 在同一台伺服器上管理多個站點?

  • April 7, 2022

我在同一個 Nginx 伺服器上有 2 個站點。

我的第一個站點https://www.example.com以下所有地址都必須重定向到該域:

xx.xx.xx.xxx

http://example.com

http://www.example.com

https://example.com

我的第二個站點https://analytics.example.com以下地址必須重定向到此域:

http://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;
}

對於這兩個站點,我都有相同的錯誤:

在此處輸入圖像描述

ipv6only=on是多餘的,因為這是預設設置。它不需要出現在listen配置中任何位置的任何指令中。

如果存在,對於任何給定的地址和埠對,它也只能出現一次。

您應該從所有listen指令中刪除它。

該錯誤消息nginx: [emerg] duplicate listen options for...意味著您為同一個埠多次應用了一個參數。也就是說,ipv6only=on這裡程式碼中的第二個參數:

example.com:

listen [::]:80 ipv6only=on default_server;

analytics.example.com

listen [::]:80 ipv6only=on; # <- this is duplicate and raises the error

線索隱藏在nginx 文件中:

listen 指令可以有幾個特定於與套接字相關的系統呼叫的附加參數。這些參數可以在任何監聽指令中指定,但對於給定的地址:埠對只能指定一次。

這適用於任何“附加”參數(如deferredreuseportso_keepalive等)。當然,如前所述,您根本不需要指定ipv6only=on

那麼最好的做法是在你的第一個或 default_server 聲明中指定任何參數。可以假設後續listen指令將繼承這些指令。

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