Menu Search Me

Nginx

Created by JimmyXu on 2020-08-10

Installing on Windows

https://nginx.org/en/docs/windows.html

Configuration

https://nginx.org/en/docs/beginners_guide.html#control

Running

Do the following commands under ./nginx-1.14.0

To start nginx

start nginx

To stop nginx

nginx -s stop

To check if nginx is running

tasklist /fi "imagename eq nginx.exe"

To reload nginx after you have made conf changes

nginx -s reload

Nginx Compression

Add the following contents:

gzip on;

gzip_vary on;

gzip_min_length 10240;

gzip_proxied expired no-cache no-store private auth;

gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;

gzip_disable "MSIE [1-6]\.";

Here's an explanation for the configuration, line by line:

·        gzip on; - enables gzip compression

·        gzip_vary on: - tells proxies to cache both gzipped and regular versions of a resource

·        gzip_min_length 1024; - informs NGINX to not compress anything smaller than the defined size

·        gzip_proxied - compress data even for clients that are connecting via proxies (here we're enabling compression if: a response header includes the "expired", "no-cache", "no-store", "private", and "Authorization" parameters)

·        gzip_types - enables the types of files that can be compressed

·        gzip_disable "MSIE [1-6]\."; - disable compression for Internet Explorer versions 1-6

 

Reference: https://www.techrepublic.com/article/how-to-configure-gzip-compression-with-nginx/

Nginx Location Directive Examples

Match All - The following will serve all URLs.

    location / {

       ......

    }

Exact Match - The following will serve only the URL http://mydomain.com/ as = means exact match.

    location =/ {

       ......

    }

Likewise, the following will serve only the URL http://mydomain.com/50x.html as = means exact match.

    location = /50x.html {

       ......

    }

Directory Match - The following will serve only the URLs that begin with http://mydomain.com/js/ and nothing else.

    location /js/ {

       ......

    }

Prefix Match - The following will serve URLs that begin with http://mydomain.com/cp- and nothing else, like /cp-test-launch, /cp-case, /cp-mypage.html, etc.

    location ^~ /cp- {

       ......

    }

Case-Sensitive Regular Expression Match - The following will match files ending with the suffixes specified no matter where they appear in the URL (case-sensitive)

    location ~ .(png|gif|ico|jpg|jpeg)$ {

       ......

    }

Case-In-Sensitive Regular Expression Match - The following will match files ending with the suffixes specified no matter where they appear in the URL (case-insensitive)

    location ~* .(png|gif|ico|jpg|jpe?g)$ {

        ......

    }

Reference: https://www.thegeekstuff.com/2017/05/nginx-location-examples/