为什么这个 NGINX 配置文件无效?
Why is this NGINX config file invalid?
所以我有这个 NGINX 配置文件:
events { }
http {
server {
listen 443 ssl;
ssl_certificate /home/dietpi/certs/cert.pem;
ssl_certificate_key /home/dietpi/certs/privkey.pem.key;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:3001/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}
它是 Docker 容器的反向代理。它为我做 SSL。我想将 HTTP 重定向到 HTTPS,所以我尝试添加:
events { }
http {
server {
listen 80 default_server;
server _;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
ssl_certificate /home/dietpi/certs/cert.pem;
ssl_certificate_key /home/dietpi/certs/privkey.pem.key;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:3001/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}
但现在它不起作用(sudo nginx -t
失败)。
它是这样说的:
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:5
nginx: configuration file /etc/nginx/nginx.conf test failed
大多数遇到此错误的人倾向于让服务器块不在 http
块中,将其放在一个块中即可修复。但是...我的 server
块已经在 http
块中。那我该如何解决呢?
server {}
块中的错别字是 server
而不是 server_name
server {
listen 80 default_server;
server_name _; # ✅ corrected
}
所以我有这个 NGINX 配置文件:
events { }
http {
server {
listen 443 ssl;
ssl_certificate /home/dietpi/certs/cert.pem;
ssl_certificate_key /home/dietpi/certs/privkey.pem.key;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:3001/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}
它是 Docker 容器的反向代理。它为我做 SSL。我想将 HTTP 重定向到 HTTPS,所以我尝试添加:
events { }
http {
server {
listen 80 default_server;
server _;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
ssl_certificate /home/dietpi/certs/cert.pem;
ssl_certificate_key /home/dietpi/certs/privkey.pem.key;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:3001/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}
但现在它不起作用(sudo nginx -t
失败)。
它是这样说的:
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:5
nginx: configuration file /etc/nginx/nginx.conf test failed
大多数遇到此错误的人倾向于让服务器块不在 http
块中,将其放在一个块中即可修复。但是...我的 server
块已经在 http
块中。那我该如何解决呢?
server {}
块中的错别字是 server
而不是 server_name
server {
listen 80 default_server;
server_name _; # ✅ corrected
}