eugene
eugene

Reputation: 41745

Nginx Is it ok to not to set server_name?

I'd like to test the server before deploying a docker image.

If I have server_name set in nginx, When I test, it redirects to the url specified in the server_name and I don't want that happen.

Also I need to have redirection rule such as

server {

     server_name www.example.kr;
     return 301 $scheme://example.kr$request_uri;
} 

So How do I make the test server not to redirect to the real www.example.com and also have www.example.com to redirect to example.com ?

Upvotes: 0

Views: 157

Answers (1)

Andrea
Andrea

Reputation: 335

You could put an IF checking hostname and redirecting it to real server, then comment out server_name block and use it as default server (not sure it could make sense as dns could or should be different between test and production server)

Configuration could be like

server {
    if ($host = www.example.com) {
        return 301 http://$host$request_uri;
    }     
    listen 80 default_server;
    listen [::]:80 default_server;
    #server_name www.example.com;
    location / {
    .. your stuff
    }
}

Upvotes: 1

Related Questions