Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is this NGINX config file invalid?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

Typo is server instead of server_name within the server {} block

server {
  listen 80 default_server;
  server_name _;   # ✅ corrected
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading