Nginx configurations file examples

After installed Nginx on a server. There’s a folder /etc/nginx/conf.d

All later configuration files which is has .conf at the end of filename will be placed in that folder

So let’s started with very beginer config file

I usually name config file with pattern mydomain.conf. So here is an example

yourdomain.com.conf

map $http_upgrade $connection_upgrade {
    default     update;
    ''          closed;
}

server {
    server_name yourdomain.com;
    location / {

        proxy_pass         http://127.0.0.1:5000/;
        proxy_set_header   Host         $host;
        proxy_set_header   X-Real-IP    $remote_addr;
        proxy_redirect     off;
        proxy_buffering    off;
        proxy_http_version  1.1;
        proxy_set_header    Upgrade     $http_upgrade;
        proxy_set_header    Connection  $connection_upgrade;
    }
}

Place this file in /etc/nginx/conf.d/

Run the following command to take effect of your configuration

sudo nginx -s reload

Example

redirect_other_domain.conf

server {
    listen 80;
    server_name  your-old-domain.com;
    rewrite ^(.*) your-new-domain.com$1 permanent;
}

server {
    server_name  your-new-domain.com;
    #charset koi8-r;
    allow all;
    location = /favicon.ico { access_log off; log_not_found off; }
    location / {
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://127.0.0.1:8000/;
    }
}

Allow websocket connection via domain

map $http_upgrade $connection_upgrade {
    default     update;
    ''          closed;
}

upstream websocket {
    server server-ip-address:port;
}

server {
    server_name your-domain.com;

    #charset koi8-r;
    allow all;

    location = /favicon.ico { access_log off; log_not_found off; }
    
    location /ws/ {
        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Proxy "";
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 86400;
    }
    
    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header    Upgrade     $http_upgrade;
        proxy_set_header    Connection  $connection_upgrade;
        proxy_pass http://websocket;
    }
}

Allow access static files

server {
    server_name  yourdomain.com;

    #charset koi8-r;
    allow all;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        alias path-to-static-folder;
    }

    location /media/ {
        alias path-to-media-folder;
    }

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://127.0.0.1:8000/;
    }
}