Blog

What is: NGINX – A Web Server and A Proxy

You must be wondering that you have heard of Nginx as a web server (ie. Apache) but A Proxy, what’s that ?? 

So, firstly who created NGINX Web Server?
–> Same developers who contributed to build Apache web server have built NGINX

Why NGINX ? 
–> It was designed to overcome flows of Apache web server
–> It has a lot more functionalities and features to serve your web better and in a secure way
–> It is more than just a web server, a proxy as an example. 

What are some benefits NGINX provides you?

  • Do you want to throttle requests that are being made at very higher rate? 
  • Do you easily want to block some IPs who are requesting more than expected? (DDoS)
  • Do you want to setup a reverse proxy where you need not to show your actual URL?
  • Do you want to host multiple websites from same server?
  • Do you want to manage user’s load 
  • Do you want to cache some objects to serve faster? 
  • Do you want to manipulate headers with REGEX? 

All of the above can be achieved with NGINX. This is just to start with, Nginx can do many more things than this. 

Let’s see some configuration examples for NGINX

  • Basic Server Block 
    • This is where you will define rules for any port/server_name configurations

server {
    listen       80;
    server_name  website.com;
    root /usr/share/nginx/html/;    
    index index.php index.html;
    client_max_body_size 50M;


    location ~* \favicon.ico$ {
        expires 6d;
    }

    location ~* \.(eot|otf|ttf|woff|woff2)$ {
        add_header Access-Control-Allow-Origin *;
        expires 6d;
    }

    location ~ \.css {
        root /usr/share/nginx/html;
        add_header  Content-Type    text/css;
        expires 6d;
    }

    location ~* \.js {
        root /usr/share/nginx/html;
        add_header  Content-Type    application/x-javascript;
        expires 6d;
    }
    location ~* \.png|jpeg|jpg {
        root /usr/share/nginx/html;
        add_header  Content-Type    image/png;
        add_header  Content-Type    image/jpeg;
        add_header  Content-Type    image/jpg;
        expires 6d;
    }
    location ~* \.svg {
        root /usr/share/nginx/html;
        add_header  Content-Type    image/svg+xml;
        expires 6d;
    }


    #this sets proxy URL, assume you want to redirect website.com/api to       #google.com, follow this rules
    location /api/ {
        proxy_pass https://google.com;
    }

   

    #NGINX rules for codeignitor structure-1 
    #for project’s Admin folder

    location /projectname/admin/ {
        try_files $uri $uri/ /projectname/admin/index.php;
        if (!-e $request_filename){
            rewrite ^(.*)$ /projectname/admin/index.php?/$1 last;
    }}
    
   
    # laravel configuration rules
     
    location /project2/public/ {
        try_files $uri $uri/ /project2/public/index.php$is_args$args;
    }
    
    #reactjs and angularjs configuration files
    # if you are getting 404 on refresh, you need to follow below mentioned structure
    
    location /project3/ {
        index  index.html;
       try_files $uri /project3/index.html =404;
    }
    # redirect users for a particular URL from http to https

    location /project4/ {
        return 301 https://website.com/project4;
    }
    
    #wordpress rules (basic)
    location /project5 {
        try_files $uri $uri/ /project5/index.php?$args;
    }
    
    
    location ~ /\.(?!well-known).* {
        deny all;
    }

    #location block to integrate PHP-FPM with NGINX

    location ~* \.php$ {
	root /usr/share/nginx/html;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        try_files $uri =404;
        fastcgi_param  REQUEST_URI      $request_uri;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
        fastcgi_pass php:9000; # here php is docker container’s name, 9000 is internal port to php’s container
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    }
}

Comments specify how every location block behaves and for which purpose that can be used.

1 thought on “What is: NGINX – A Web Server and A Proxy”

Leave a Comment