Nginx
nginx:emerg____和米和rGemerg此處不允許使用“位置”(/“伺服器”)指令
我嘗試為兩個域創建 2 個單獨的 NGINX conf 文件。每個域都有一個測試。出於規範原因,子域和 www 應永久重定向到 now-www。這樣做的正確方法是什麼?我在下面的程式碼中也出現錯誤。
sudo nginx -T nginx: [emerg] "location" directive is not allowed here in /etc/nginx/sites-enabled/example.com:33
這是 example.com 的我的 conf 文件,example2 的第二個文件幾乎相同。
server { listen 80; listen [::]:80; root /var/www/example.com/html; index index.php index.html index.htm index.nginx-debian.html; server_name example.com 123.456.7.8; #(For WordPress permalinks) try_files $uri $uri/ /index.php$is_args$args; } # Redirect all traffic to www to non-www for SEO canonical reasons server { listen 80; listen [::]:80; server_name www.example.com; location / { return 301 https://www.example.com/$request_uri; } } # Direct all traffic to the subdomain to a separate folder server { listen 80; listen [::]:80; root /var/www/test/example.com/html; index index.php index.html index.htm index.nginx-debian.html; server_name test.example.com; #(For WordPress permalinks) try_files $uri $uri/ /index.php$is_args$args; } # include /etc/nginx/naxsi.rules location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.4-fpm.sock; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { log_not_found off; access_log off; allow all; } location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ { expires max; #To enable leverage browser caching log_not_found off; }
您的配置中有語法錯誤
…您收到的兩條錯誤消息都應該清楚。
如果您在配置中使用了縮進,問題將變得明顯且易於發現:
- 從
server{}
第 13 行開始的部分沒有結尾}
。- 此外,如果您只是添加了
}
第 20 行,第location{}
33-48 行的部分將成為孤立的。帶縮進的原始配置
您收到此錯誤消息,指出第 22 行的問題。
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-enabled/example.com:22
這不是一個解決方案,而只是為了展示目前配置是如何解釋的:
01: server { 02: listen 80; 03: listen [::]:80; 04: root /var/www/example.com/html; 05: index index.php index.html index.htm index.nginx-debian.html; 06: server_name example.com 123.456.7.8; 07: 08: #(For WordPress permalinks) 09: try_files $uri $uri/ /index.php$is_args$args; 10: } 11: 12: # Redirect all traffic to www to non-www for SEO canonical reasons 13: server { 14: listen 80; 15: listen [::]:80; 16: server_name www.example.com; 17: location / { 18: return 301 https://www.example.com/$request_uri; 19: } 20: 21: # Direct all traffic to the subdomain to a separate folder 22: server { 23: listen 80; 24: listen [::]:80; 25: root /var/www/test/example.com/html; 26: index index.php index.html index.htm index.nginx-debian.html; 27: server_name test.example.com; 28: 29: #(For WordPress permalinks) 30: try_files $uri $uri/ /index.php$is_args$args; 31: } 32: 33: # include /etc/nginx/naxsi.rules 34: location ~ \.php$ { 35: include snippets/fastcgi-php.conf; 36: fastcgi_pass unix:/run/php/php7.4-fpm.sock; 37: } 38: location = /favicon.ico { 39: log_not_found off; access_log off; 40: } 41: location = /robots.txt { 42: log_not_found off; access_log off; allow all; 43: } 44: location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ { 45: expires max; 46: 47: #To enable leverage browser caching log_not_found off; 48: }
您第一次嘗試解決此問題
…通過添加
}
(第 20 行)沒有幫助,因為它沒有考慮我的第二個建議!nginx: [emerg] "location" directive is not allowed here in /etc/nginx/sites-enabled/example.com:33
現在,您仍然有孤立的
location{}
指令,在第 34-47 行:13: server { 14: listen 80; 15: listen [::]:80; 16: server_name www.example.com; 17: location / { 18: return 301 https://www.example.com/$request_uri; 19: } 20: } 21: # Direct all traffic to the subdomain to a separate folder 22: server { 23: listen 80; 24: listen [::]:80; 25: root /var/www/test/example.com/html; 26: index index.php index.html index.htm index.nginx-debian.html; 27: server_name test.example.com; 28: 29: #(For WordPress permalinks) 30: try_files $uri $uri/ /index.php$is_args$args; 31: } 32: 33: # include /etc/nginx/naxsi.rules 34: location ~ \.php$ { 35: include snippets/fastcgi-php.conf; 36: fastcgi_pass unix:/run/php/php7.4-fpm.sock; 37: } 38: location = /favicon.ico { 39: log_not_found off; access_log off; 40: } 41: location = /robots.txt { 42: log_not_found off; access_log off; allow all; 43: } 44: location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ { 45: expires max; 46: #To enable leverage browser caching log_not_found off; 47: }
解決方案是將這些
location{}
部分放在其中一個server{}
部分中。這裡沒有人能說出哪一個對他們來說是正確的,因為您沒有在您的問題中解釋這一點:它們與您關於在單獨文件中處理兩個域的問題完全無關。最後,清晰的模板配置
此配置範例嘗試解決您的問題,因為它在標題中。如果您
/etc/nginx/sites-enabled/example.com
將/etc/nginx/sites-enabled/example.net
所有的example.com
替換example.net
為server { listen 80; server_name example.com www.example.com test.example.com; # HTTP to HTTPS redirections for all the subdomains return 301 https://$host$request_uri; } server { listen 443 ssl; server_name www.example.com; # ssl_* directives here # www to non-www for SEO canonical reasons return 301 https://example.com$request_uri; } server { listen 443 ssl; server_name example.com; # ssl_* directives here root /var/www/example.com/html; } server { listen 443 ssl; server_name test.example.com; # ssl_* directives here root /var/www/example.com/test; }
只需
location{}
根據需要在此處添加部分。我還刪除了listen
IPv6 的附加指令、index
指令等,因為它們可能使您蒙蔽了雙眼。一旦你消除了所有的噪音,解決方案就很簡單了,不是嗎。然後,您可以微調其餘部分。