我尝试过
nginx -t
并且一切看起来都正确。
我收到此错误:
nginx -c /etc/nginx/sites-available/premonstrati2021.cz -T
nginx: [emerg] "index" directive is not allowed here in /etc/nginx/sites-available/premonstrati2021.cz:1
nginx: configuration file /etc/nginx/sites-available/premonstrati2021.cz test failed
我的配置文件是这样的:
index index.html index.htm;
server {
listen 80;
listen [::]:80;
server_name premonstrati2021.cz;
#port_in_redirect off;
#proxy_set_header Host $host:$server_port;
# if ($scheme != "https") {
# return 301 https://$host$request_uri;
# }
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
root /var/www/html/app/dist;
server_name premonstrati2021.cz www.premonstrati2021.cz;
ssl_dhparam /etc/nginx/dhparam.pem
ssl_certificate "/etc/letsencrypt/live/premonstrati2021.cz/cert.pem";
ssl_certificate_key "/etc/letsencrypt/live/premonstrati2021.cz/privkey.pem";
ssl_verify_client optional;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE$
access_log /var/log/nginx/log/premonstrati.access.log main;
error_log /var/log/nginx/log/premonstrati.com.br.error.log;
port_in_redirect off;
proxy_set_header Host $host:$server_port;
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://127.0.0.1:3000/api;
proxy_ssl_server_name on;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /widget {
root /var/www/html/app/widget;
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
}
}
server {
listen 443;
server_name www.premonstrati2021.cz;
return 301 https://$server_name$request_uri;
}
我无法重新启动 nginx,因为出现错误,但我找不到任何错误。我刚刚添加了带有 location /widget { ... } 的块。我试图删除它,但没有成功。你有什么想法吗?
答案1
该指令index index.html index.htm;
应该放在上下文内部server {}
,但是你却把它放在任何之外server
。
将其移至适当的上下文,例如:
server {
listen 80;
listen [::]:80;
server_name premonstrati2021.cz;
index index.html index.htm;
#port_in_redirect off;
#proxy_set_header Host $host:$server_port;
# if ($scheme != "https") {
# return 301 https://$host$request_uri;
# }
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
root /var/www/html/app/dist;
server_name premonstrati2021.cz www.premonstrati2021.cz;
ssl_dhparam /etc/nginx/dhparam.pem
ssl_certificate "/etc/letsencrypt/live/premonstrati2021.cz/cert.pem";
ssl_certificate_key "/etc/letsencrypt/live/premonstrati2021.cz/privkey.pem";
ssl_verify_client optional;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE$
access_log /var/log/nginx/log/premonstrati.access.log main;
error_log /var/log/nginx/log/premonstrati.com.br.error.log;
port_in_redirect off;
proxy_set_header Host $host:$server_port;
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://127.0.0.1:3000/api;
proxy_ssl_server_name on;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /widget {
root /var/www/html/app/widget;
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
}
}
server {
listen 443;
server_name www.premonstrati2021.cz;
return 301 https://$server_name$request_uri;
}
答案2
您需要创建一个从文件/etc/nginx/sites-available/premonstrati2021.cz
到的符号链接,/etc/nginx/sites-enabled/premonstrati2021.cz
以便 nginx 实际使用您的配置。
目前,您的配置位于 下的文件中sites-available
,但 nginx 不包含该文件中的文件。
另外,这个块是错误的:
server {
listen 443;
server_name www.premonstrati2021.cz;
return 301 https://$server_name$request_uri;
}
这会导致无限的 301 重定向循环。由于 已存在阻止server
,因此www.premonstrati2021.cz
您只需删除此阻止即可。