So I have this NGINX config file:
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";
}
}
}
It is a reverse-proxy for a Docker container. It does SSL for me. I wanted to redirect HTTP to HTTPS, so I tried adding this:
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";
}
}
}
But now it doesn't work (sudo nginx -t
fails).
It says this:
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:5
nginx: configuration file /etc/nginx/nginx.conf test failed
Most people that have this error tend to have the server block not in an http
block, and putting it in one fixes it. But... my server
block is already in an http
block. So how do I fix it?
CodePudding user response:
Typo is server
instead of server_name
within the server {}
block
server {
listen 80 default_server;
server_name _; # ✅ corrected
}