Skip to content

Sample nginx configuration

Johnny Richardson edited this page Oct 29, 2016 · 8 revisions

Also viewable as a gist.

Reverse proxy config for app server

# Based partially on: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04

# App running on port 8000)
server {

    listen 80;

    server_name www.domain.org domain.org;

    # enable gzip
    gzip on;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_min_length  1000;
    gzip_proxied any;
    gzip_buffers 16 8k;

    # we only gzip these mime-types (since there's no use to gzip jpegs)
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    location / {
       proxy_pass http://127.0.0.1:8000;
       proxy_http_version 1.1;

       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection 'upgrade';
       proxy_set_header Host $host;

       proxy_cache_bypass $http_upgrade;
    }

}

# HTTP server at port 80 (for redirection to https)
server {

     listen 80;
     server_name subdomain.domain.edu www.subdomain.domain.edu www.domain.edu;

     # Required for SSL auto-renew
     location '/.well-known/acme-challenge' {

        default_type "text/plain";
        root /tmp/letsencrypt-auto;

      }

      # All other traffic
      location / {

        ## redirect http to https ##
        rewrite ^ https://$server_name$request_uri? permanent;

      }

}

# Map a url to sub-page (port 3000/subpage1)
server {

    listen 80;

    server_name www.domain2.edu domain2.edu;

    location / {
       proxy_pass http://127.0.0.1:3000/subpage1;
       proxy_http_version 1.1;

       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection 'upgrade';
       proxy_set_header Host www.domain.edu;

       proxy_cache_bypass $http_upgrade;
    }
}

# Another way to map sub-page to external URL,
# forcing nginx to redirect all resource request to primary domain (port 3000/subpage1/subpage2/)
server {

    listen 80;

    server_name domain3.net www.domain3.net;

    location / {
       # debug-only log
       access_log /var/log/nginx/domain3.access.log;
       proxy_pass http://127.0.0.1:3000/subpage1/subpage2/;
       proxy_http_version 1.1;

       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection 'upgrade';
       proxy_set_header Host www.domain.edu;

       proxy_cache_bypass $http_upgrade;

    }

    # Redirect all asset urls to correct domain
    location ~ ^/(.+) {
       return 301 https://www.domain.edu/;
    }

}


# A secure website (port 3000)
server {

    listen 443 ssl;

    # enable gzip
    gzip on;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_min_length  1000;
    gzip_proxied any;
    gzip_buffers 16 8k;

    # we only gzip these mime-types (since there's no use to gzip jpegs)
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ssl_certificate /etc/letsencrypt/live/domain.edu/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domamin.edu/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/domain.edu/chain.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA2$DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;

    server_name internal_subdomain.emerson.edu;
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;

        proxy_cache_bypass $http_upgrade;

        client_max_body_size 500M;
    }

}
Clone this wiki locally