Nginx Location Directive

Nginx Location Directive is used to route request to correct files.

Match

Exact match is used to match an exact URL.

When location is used with no modifiers, then beginning of the URL is matched. In this case, any url http://domain/ok/FILE_NAME will be served from /home/ok/FILE_NAME

Exact Match (=)

Exact match is used to match an exact URL.

In this example http://domain/ok/index.html get served from /home/ok/index.html. Only this specific file will be matched.

Cause Insensitive Regular Expression Match (~*)

Above code routes URL http://domain/ok/ to /home/ok/index.html. But won’t match http://domain/OK/.

If you need both /ok and /OK work, you need to use

With this config, http://domain/OK/FILE will be served from /home/OK/FILE.

See Nginx

nginx password protect

Nginx Password Protect a website

nginx password protect

To password protect a web site, you need to install htpasswd utility. On Ubuntu/Debian, you can install it with command

Now create a password file with command

It will ask for password.

Edit configuration file for your web site and add following in the server entry for the web site.

Restart Nginx.

Now on visiting the web site, you will be asked to enter username and password.

See Nginx

Configure Nginx to listen on single IP Address

By default Nginx listens on all IP address on a server. To make nginx listen on specific IP address, edit nginx configuration file

And VirtualHost/server files for each domain located in folders

Find

Replace with

IP_ADDR_HERE = your server IP address on which you need nginx listen on.

See Nginx

Nginx Web Server

Nginx Config for Laravel Application in sub folder

To run Laravel Application on sub folder of a web site, use following configuration. If you run Laravel application as main site, see Nginx Config for Laravel Application

Here you place Laravel application in a subdirectory “subFolderApp1”.

Example

Nginx Config for Laravel Application

Here is Nginx configuration for a laravel application

Nginx Config for Laravel Application in sub folder

nmap

Disable TLSv1 in Nginx

To disable TLSv1 in nginx, add

in your server config.

if you are using letsencrypt SSL, edit file

Find

Replace with

Restart Nginx

To verify, run

This will list all supported SSL protocols.

nmap

Nginx Web Server

Nginx Rails Origin header didn’t match request.base_url

After installing SSL on Nginx server, rails application login page stopped working.

On log file (log/production.log), found following error

HTTP Origin header (https://domain.com) didn't match request.base_url (http://domain.com)

The Nginx config used was

The problem is solved by adding following to nginx config.

The new config is

See Nginx

Nginx Web Server

Nginx Disable Access log

On a high traffic web site, i want to disable access log as we are hitting I/O Limit. Since we don’t use this access log for anything now, there is no point keep writing it to a file. To disable, you need to add following to server entry for your web site.

Here is an example nginx config with access logs disabled.

Enable Directory Listing in Nginx

To enable directory listing in Nginx, add following to server configuration.

Example

If you need it for a specific folder, add

Nginx Configuration for Video Streaming With Secure Link

here is nginx configuration i used for a video site that have secure link.

This video server is a storage server in vShare Youtube Clone script.