nginx – get parsed server_name

Christos Baziotis asked:

I have created virtual hosts for my websites and then i have created an other virtual host with server_name -> default_server in order to catch everything else and show a specific page in that case (ex. if someone tries to access my ip).

Also i use phpmyadmin, roundcube and squirrelmail and i wanted to redirect any connection to them from http to https. And here is the problem:

Lets say i type: (assume is my ip)

Because i have set server_name -> default_server

server {

    listen 80;

    server_name default_server;

    root /usr/share/nginx/www;
    index index.html index.htm;

    location /phpmyadmin {
            rewrite     ^   https://$server_name$request_uri? permanent;


The url to which i am redirected is not but https://default_server/phpmyadmin which of course doesn’t exist.

So i ended up pointing explicitly to my ip:

location /phpmyadmin {
        rewrite     ^$request_uri? permanent;

It works like this, but i was wondering if there is a way to do the same thing using a variable that returns the parsed server_name or any other / smarter way.

My answer:

Use $http_host instead of $server_name.

            rewrite     ^   https://$http_host$request_uri? permanent;

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.