Wednesday, October 30, 2024
HomeLinux GeneralSet Up Nginx as a Reverse Proxy

Set Up Nginx as a Reverse Proxy

Based on the web server’s operating system, you can set up Nginx in different ways. Here we will show you by installing the primary site at example.com domain name, while the proxied WordPress site is installed at abc.domain.com subdomain. Let’s see how to install and configure Nginx as a reverse proxy on the main server.

First, access your server’s terminal via SSH and update your distribution’s packages list and then install Nginx on your web server by using the command apt-get.

sudo apt update
sudo apt install nginx

Now create a new virtual host file, to configure Nginx to proxy requests for domains hosted on Apache. Below is an example of using a nano editor to add the code, if you want you can choose any code editor.

sudo nano /etc/nginx/sites-available/example.com.conf

Next, you need to set Nginx directives so you can forward requests to Apache.

server {
listen      80;
server_name example.com www.example.com;
index       index.php;
root        /var/www/example.com/public    #fallback for index.php
location / 
try_files $uri $uri/ /index.php?$query_string;
location /abc 
proxy_pass http://abc.domain.com;proxy_http_version                 1.1;
proxy_cache_bypass                 $http_upgrade;
# Proxy headers
proxy_set_header Upgrade           $http_upgrade;
proxy_set_header Connection        "upgrade";
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;
proxy_set_header X-Forwarded-Host  $host;
proxy_set_header X-Forwarded-Port  $server_port;
# Proxy timeouts
proxy_connect_timeout              60s;
proxy_send_timeout                 60s;
proxy_read_timeout                 60s;

Make sure you use your proxied website’s public IP address (or the URL) in the proxy_pass directive and also before you make any changes ensure the proxied website is installed and ready to be served.

Now you can save the virtual host file and activate the new virtual host.

sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf

Check for any configuration errors.

sudo nginx –t

If you did not find any error you can reload Nginx to see the changes.

sudo systemctl reload nginx

You can use the phpinfo () function to check to see if the PHP variables are loaded when you visit your proxied site.

Hope it was helpful. If you need assistance you can get here.

Also check: How to Install VestaCP on Ubuntu 18.04

To get more updates you can follow us on Facebook, Twitter, LinkedIn

Subscribe to get free blog content to your Inbox

Source

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments