Friday, June 14, 2024

Install & configure NGINX on Windows | Steps of NGINX setup on Windows

Whenever we search for installation and configuration of NGINX on Windows operating system, we generally find example of Linux or Ubantu machines. So, here I am going to discuss about installation and configuration of NGINX on windows machine. Along with it, I will also discuss about how multiple applications (running on different ports on localhost) can be configured with NGINX.

I have also created an Youtube video about this topic. You can go through the video to get more idea.

NOTE: Link of the video: https://youtu.be/h-NWXxyrkJ0?si=hr-_PZ29B_vbSfkC


STEPS of NGINX Installation:

1) Open the following URL of NGINX download website and download the zip file of latest stable version of NGINX. 
URL: https://nginx.org/en/download.html



2) Unzip the file and extract the folder wherever you want to keep your NGINX folder


3) Open CMD in Administration mode and move to the NGINX folder. In my case it is "C:\nginx-1.27.0". Then type command like "start NGINX". Open browser and type "http://localhost:80" and press Enter. Now, if below response is coming on browser then NGINX is running properly.




We can run few more commands of NGINX as per our needs:


Steps of multiple application configuration on NGINX

Suppose we have two NodeJS application running in our localhost.
  1. localhost:3000/helloworld
  2. localhost:3002/
Here, if we are running these two application on browser, we are getting response. Now, we want to configure both the application in NGINX. After configuring with NGINX, we have to start both the application separately and then restart NGINX server. After this we can access both the application through NGINX in localhost on our Windows machine.

Steps:

1) Open NGINX configuration file that is nginx.conf in Notepad or any editor:


2) Add the following configuration code in http object in nginx.conf file

upstream test_app {
        server 127.0.0.1:3000;
    }

    upstream dev_app {
        server 127.0.0.1:3002;
    }

3) Add the following code in same nginx.conf file under http.server object

location /test/ {
            proxy_pass http://test_app/;
            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;
        }

        location /dev/ {
            proxy_pass http://dev_app/;
            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;
        }

Below, is the complete nginx.conf file.

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}
   
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream test_app {
        server 127.0.0.1:3000;
    }

    upstream dev_app {
        server 127.0.0.1:3002;
    }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
       
        location /test/ {
            proxy_pass http://test_app/;
            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;
        }

        location /dev/ {
            proxy_pass http://dev_app/;
            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;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}



4) Test the code changes in nginx.conf file by command "nginx -t" in CMD. If the response of the command is success then its all good otherwise there is some syntax error that should be rectified before starting the NGINX server


5) Restart the NGINX server with command like "nginx -s reload"
  

6) Now, access both the application in browser with url like
  1. Use URL http://localhost/test instead of localhost:3000/helloworld
  2. Use URL http://localhost/dev instead of localhost:3002/