Tuesday, August 10, 2021

Setting up nginx with Node JS


NGINX is a high-performance HTTP server as well as a reverse proxy.

Unlike traditional servers, NGINX follows an event-driven, asynchronous architecture. As a result, the memory footprint is low and performance is high. If you’re running a Node.js-based web app, you should seriously consider using NGINX as a reverse proxy.



NGINX can be very efficient in serving static assets. For all other requests, it will talk to your Node.js back end and send the response to the client



To install nginx on AWS, yum can be used. 


sudo yum install nginx


Once the nginx is installed, configuration can be done on the below file. 


sudo vi /etc/nginx/nginx.conf


to start the nginx 


sudo nginx

to reload nginx 

nginx -s reload

Now at this point if type in the IP or domain, should be able to see the Amazon landing page from nginx. 


Now it is time to forward the request to the Application server, to do that , need to edit the /etc/nginx/nginx.conf


Below is a sample configuration for SSL and for forwarding all requests on Port 80 to Port 1337 where Application server is listening  


Now lets encrypt might have given in form of chain and actual cert. nginx can take cert and key form. so create new cert like this below 


cat cert.pem fullchain.pem >newcert.pem



     server {

        listen       80;

        listen       443 ssl http2 default_server;

        listen       [::]:443 ssl http2 default_server;

        server_name  _;

        root         /usr/share/nginx/html;


        ssl_certificate "/etc/nginx/ssl/newcert.pem";

        ssl_certificate_key "/etc/nginx/ssl/privkey.pem";

        ssl_session_cache shared:SSL:1m;

        ssl_session_timeout  10m;

        ssl_ciphers HIGH:!aNULL:!MD5;

        ssl_prefer_server_ciphers on;


        # Load configuration files for the default server block.

        include /etc/nginx/default.d/*.conf;



        location / {

           proxy_pass http://localhost:1337;

           proxy_http_version 1.1;

           proxy_set_header Upgrade $http_upgrade;

           proxy_set_header Connection 'upgrade';

   proxy_set_header Host $host;

           proxy_cache_bypass $http_upgrade;

        }


        error_page 404 /404.html;

        location = /404.html {

        }


        error_page 500 502 503 504 /50x.html;

        location = /50x.html {

        }

    }


                                                       



references

https://www.sitepoint.com/configuring-nginx-ssl-node-js/

https://medium.com/@mertcal/install-nginx-on-ec2-instance-in-30-seconds-e714d427b01b



No comments:

Post a Comment