PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 1, 2022

[FIXED] Why is this NGINX config file invalid?

 September 01, 2022     https, nginx, nginx-config, nginx-reverse-proxy     No comments   

Issue

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?


Solution

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

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


Answered By - AnthumChris
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing