Nginx Configuration file example for NodeJS App

Nginx Configuration file example for NodeJS App:

server {
   listen 80;
   # redirect everything to https
   return 301$request_uri;
    server {
        listen 443 ssl;
        ssl                  on;
        ssl_certificate      /etc/letsencrypt/live/;
        ssl_certificate_key  /etc/letsencrypt/live/;
        gzip on;
        gzip_types      text/plain application/xml;
        gzip_proxied    no-cache no-store private expired auth;
        gzip_min_length 1000;
		add_header X-GG-Cache-Status $upstream_cache_status;
		add_header X-GG-Cache-Date $upstream_http_date;
        rewrite ^/(.*)/$ /$1 permanent;
		access_log  /var/log/nginx/ custom_cache_log ;
    location /.well-known {
            alias /var/www/acme/.well-known;
		# NoedeJS app is running on port 3000
       location / {
            proxy_pass   ;
            proxy_set_header       Host $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;
	    # cache all requests of any type for 5m
            proxy_cache_methods HEAD GET POST;
            proxy_cache_valid any 5m;
            expires 5m;
            # default proxy cache key
            proxy_cache_key $scheme$proxy_host$request_uri;
            # on first use
            proxy_cache_min_uses 0;
	    # upgrade for nodejs socket
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_buffering        on;
            proxy_cache            STATIC;
            proxy_cache_use_stale  error timeout invalid_header updating
                                   http_500 http_502 http_503 http_504;
	# files to cache
    location ~* ^(jpg|jpeg|gif|css|png|js|ico|svg|eot|ttf|woff|woff2|)$ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
        try_files       $uri =404;
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.