Skip to main content

Nginx reverse proxy for ReactJS, Vuejs,Laravel and Django with or without Docker

Nginx is an important piece on the working chain apps so that it works as bridge between the end user and the application servers, to do that we will be using nginx as a reverse proxy

to know more in deep about nginx reverse proxy go to https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/


let's suppose you have a domain name called yourdomainame.com

Nginx - ReactJS/Vuejs

each time you need to create a new domain name, set it on the path /etc/nginx/conf.d/ create a file with vim or nano or whatever you want, and give it the domain name .conf and set the settings on it, this is really useful when you start handling several domain names under the same server

vim /etc/nginx/conf.d/yourdomainame.com.conf

server {

        server_name  yourdomainame.com wwww.yourdomainame.com;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            root /usr/share/nginx/html/reactproject/build;
            try_files $uri /index.html;
            expires -1;
        }

}


these setting work for both ReactJS and VueJS, this is in the case you only need running a frontend app without any backend

Nginx - ReactJS/Vuejs with SpringBoot without Docker

let's suppose you have backend in SpringBoot, and you have got to main prefix endpoints for your backend /api and /login and it is running over the 7078 port then you would need to do

server {

        server_name  yourdomainame.com wwww.yourdomainame.com;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            root /usr/share/nginx/html/reactproject/build;
            try_files $uri /index.html;
            expires -1;
        }
     
        location /api {
         
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header HOST $http_host;

                proxy_pass http://localhost:7078;

        }

        location /login {
         
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header HOST $http_host;

                proxy_pass http://localhost:7078;

        }
}

Nginx - ReactJS/Vuejs with SpringBoot with Docker

Let's suppose you have got already your project running with Docker, and the backend keeps working in the 7078 port  and let's suppose you you have working an nginx frontend ReactJS or VueJS docker container running in the 81 port, and you don't want to set SSL to the docker nginx, but you rather preffer having an nginx before the container docker nginx as reverse proxy for your frontend app and backend app

server {

        server_name  yourdomainame.com wwww.yourdomainame.com;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
     
                expires -1;
                proxy_set_header Host      $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-Proto  $scheme;
                proxy_redirect              off;
                proxy_read_timeout          1m;
                proxy_connect_timeout       1m;
                proxy_pass http://localhost:81;
      }
     
        location /api {
         
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header HOST $http_host;

                proxy_pass http://localhost:7078;

        }

        location /login {
         
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header HOST $http_host;

                proxy_pass http://localhost:7078;

        }
}


Nginx - Django with Docker

Let's suppose you have got already a django project running with Docker over the port 8000 and you don't want to set SSL to the docker nginx, but you rather preffer having an nginx before the container docker nginx as reverse proxy for your frontend app and backend app

server {

        server_name  yourdomainame.com wwww.yourdomainame.com;

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

        location / {

                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header HOST $http_host;
                expires -1;
                proxy_pass http://localhost:8000;
        }

}


Nginx - Django restframework with ReactJS/VueJS with Docker 

Let's suppose you have got already a django restframework project running with Docker over the port 8000 and let's suppose you have working an nginx frontend ReactJS or VueJS docker container running in the 81 port and you don't want to set SSL to the docker nginx, but you rather preffer having an nginx before the container docker nginx as reverse proxy for your frontend app and backend app

server {

        server_name  yourdomainame.com wwww.yourdomainame.com;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

      location / {
     
                expires -1;
                proxy_set_header Host      $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-Proto  $scheme;
                proxy_redirect              off;
                proxy_read_timeout          1m;
                proxy_connect_timeout       1m;
                proxy_pass http://localhost:81;
        }

        location /api {

                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header HOST $http_host;
                proxy_pass http://localhost:8000;
        }
}


Nginx - Laravel - NuxtJS using Docker

it is the same concept, if you have got your backend running for instance in the 8001 port and the front running in the 3000 both of the running over docker, but you do not want to set SSL to the docker container image for the frontend, instead of that you preffer having an nginx behind like in the main the server, then you should something like this 

server {

      server_name  yourdomainame.com wwww.yourdomainame.com;

      location /api {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header HOST $http_host;
                proxy_pass http://localhost:8001;
      }

      location / {
                expires -1;
                proxy_set_header Host      $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-Proto  $scheme;
                proxy_redirect              off;
                proxy_read_timeout          1m;
                proxy_connect_timeout       1m;
                proxy_pass http://localhost:3000;
      }
}


Note: SSL can be handle through many ways, with AWS, CloudFlare or manual buying the certificates with many companies, this one is quite well https://www.namecheap.com/security/ssl-certificates or you can handle the SSL in an automatic way with certbot tool, I like a lot certbot so that it makes everything for me just giving it the email administrator and the domain name and that's it !, it makes the redirections in the nginx config file which the domain is, just running:

certbot certonly --noninteractive --agree-tos --cert-name ${SITE_TLD} -d ${SITE_DOMAIN_ONE} -d ${SITE_DOMAIN_TWO} -m ${SSL_EMAIL}

certbot --nginx --noninteractive --agree-tos --cert-name domainname.com -d domainname.com -d www.domainname.com -m webmaster@domainname.com

NOTE: so having this said, it is assumed you will set the SSL to your domain name over the possible ways you want to get, if you prefer using AWS or CloudFlare do not set SSL to the nginx unless you want to secure the traffic between the load balancer and your instance, if you want to use it in a manual way then you've got the option about buying the SSL or using certbot


Comments

Popular posts from this blog

How to deploy a VueJS App using Nginx on Ubuntu

There are thousands of blogs and websites out there explaining how to do a hello world and how to start with VueJS, but in this little post, I’m just going to be explaining how to do deploy a VueJs app after you have run the command through the CLI npm run build . So when you run the command npm run build a dist folder is created and that folder’s got the essential .js files to run our app, when we want to run our app on an Nginx server by default the vue-router is not going to work well so that Nginx does not come ready to work by default with a VueJs app This is basically for a Linux Ubuntu distribution if you’ve got any other Linux distribution just pay attention where is going to be set the www/html folder and the Nginx settings so that this is useful for any Linux distribution  Install and configure nginx sudo apt install nginx Double check to make sure the nginx service is running with command service nginx status, then open your browser and enter url

How to do pagination SpringBoot with Jbctemplate, MySQL

We are going to be working on a topic which is a basic need when doing any app, and it is Pagination. let's get started creating a product table at https://mockaroo.com/ create table products ( id INT, name VARCHAR(50), code VARCHAR(50) ); insert into products (id, name, code) values (1, 'Hettinger-Goyette', '42549-680'); insert into products (id, name, code) values (2, 'Konopelski-Klein', '49527-724'); insert into products (id, name, code) values (3, 'Smitham, Kuhlman and Balistreri', '53238-003'); insert into products (id, name, code) values (4, 'Hettinger, Weissnat and Goodwin', '0143-9916'); insert into products (id, name, code) values (5, 'Rowe Inc', '42291-898'); insert into products (id, name, code) values (6, 'Ernser-Hauck', '10544-617'); insert into products (id, name, code) values (7, 'Maggio and Sons', '68788-9087'); insert into products (id, name,

How to secure SpringBoot with SSL and Tomcat or Undertow

when we are going to take our applications to production mode, we must have an SSL certificate for the FrontEnd and   BackEnd too, in this case, our backend is a  SpringBoot which is using a tomcat embedded server. Terminology TLS vs SSL TLS is the successor to SSL. It is a protocol that ensures privacy between communicating applications. Unless otherwise stated, in this document consider TLS and SSL as interchangable. Certificate (cert) The public half of a public/private key pair with some additional metadata about who issued it etc. It may be freely given to anyone. Private Key A private key can verify that its corresponding certificate/public key was used to encrypt data. It is never given out publicly. Certificate Authority (CA) A company that issues digital certificates. For SSL/TLS certificates, there are a small number of providers (e.g. Symantec/Versign/Thawte, Comodo, GoDaddy, LetsEncrypt) whose certificates are included by most browsers and Operating System