Using Nginx as Proxy Server for Laravel or Any Webapp

using nginx as proxy server#

Nginx is web server which we use to deploy our web app on VPS server. we can map our domain to particular app directory and even can be used for proxy server or load balancing.

using nginx as proxy server we have to use proxy_pass variable to redirect all traffic to local app server.

first we will create server block and then server name which is our domain name.

below is the basic config for nginx it will listen on port 80 and match the domain name with example.com. and redirect all the traffic to localhost:8000.


server {

    listen 80;

    server_name your_domain_name.com;

    location / {
        proxy_pass http://localhost:8000;
    }


}

for complete configuration for laravel or any php application below config will work.


server {

    listen 80;

    server_name example.com;

    location / {
         proxy_set_header X-Real-IP $remote_addr;
	     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	     proxy_set_header Host $host;
         proxy_set_header X-Forwarded-Proto $scheme;
	     proxy_pass http://localhost:8000;
    }


}



use below config for nginx configuration for magento 2.4 app as reverse proxy#


server {

	listen 80;

	server_name your_domain_name.com;

	location / {
		
		add_header 'Access-Control-Allow-Origin' 'http://your_domain_name.com';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
        add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';

        if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' 'http://cregga.com';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
        add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
        }

        proxy_redirect off;
        proxy_set_header host $host;
        proxy_set_header X-real-ip $remote_addr;
        proxy_set_header X-forward-for $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 200s;

		proxy_pass http://localhost:8000;

		proxy_buffer_size   128k;
		proxy_buffers   4 256k;
		proxy_busy_buffers_size   256k;
	}


}


if you want to activate https it is very easy you can use letsencrypt certificate via certbot.

for installing certbot in ubuntu 21.04 or lower version use the below command

sudo apt install certbot python2-certbot-apache

for nginx server use nginx instead of apache

sudo apt install certbot python2-certbot-nginx

for ubuntu 22.04 use the snap package because above method will not work. i wasn’t able to do so i have used snap

sudo snap install certbot

after installing certbot you just have to run certbot command and it will read the domain from configuration and ask some question and automatically deploy the certificate you do not have to manually add certificate to nginx configuration.

sudo certbot
comments powered by Disqus