1. Introduction
Nginx is a high-performance HTTP and reverse proxy server, and is also an IMAP/POP3/SMTP proxy server. This document will explain how to configure request forwarding using Nginx and explain some common configuration parameters.
2. Nginx installation
Before configuring, make sure your system has Nginx installed. If not installed, you can use the following command to install:
On CentOS/RHEL:
sudo yum install nginx -y
On Ubuntu/Debian:
sudo apt-get update sudo apt-get install nginx -y
After the installation is complete, start Nginx with the following command:
sudo systemctl start nginx
3. Configuration file path
The main configuration file for Nginx is usually located in /etc/nginx/. In addition, site-level configuration files can be placed in the /etc/nginx// directory.
4. Configure forwarding
4.1 Basic configuration
Here is a basic Nginx request forwarding configuration example that listens to a port of the server and forwards the request to the specified target server.
server { listen 8888; # Listen to the 8888 port of the local server server_name _; # Wild all host headers location / { proxy_pass http://192.168.101.30:9000; # Forward the request to the target server proxy_set_header Host $host; # Keep the original host header proxy_set_header X-Real-IP $remote_addr; # Get the real IP address of the client proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # Support X-Forwarded-For header proxy_set_header X-Forwarded-Proto $scheme; # Support X-Forwarded-Proto header to indicate the request protocol } }
4.2 Parameter explanation
listen: Specify the port to which Nginx listens. In the example above, Nginx listens to port 8888.
server_name: Used to match client requests
Host
Header information. Can be set to a specific domain name, IP address or use_
Wildcards match all requests.location: Defines how Nginx handles requests for specified paths. In this case,
/
Indicates matching all paths.proxy_pass: Specify the backend server to which requests are to be forwarded. It can be a URL or an upstream server group.
-
proxy_set_header: These instructions are used to set the request header passed to the backend server:
- Host: Keep the host header in the client request.
- X-Real-IP: Get the real IP address of the client and pass it to the backend server.
- X-Forwarded-For: Record the proxy server IP address link that the request passes.
- X-Forwarded-Proto: Indicates the protocol (HTTP or HTTPS) used by the request.
5. Reload the configuration
After each modification of the Nginx configuration file, the configuration needs to be reloaded to take effect:
sudo nginx -s reload
You can use the following command to test whether the syntax of the Nginx configuration file is correct:
sudo nginx -t
6. Sample Scenario
6.1 Scenario 1: Port Forwarding
Forward all requests from the local server port 8888 tohttp://192.168.101.30:9000
。
server { listen 8888; server_name _; location / { proxy_pass http://192.168.101.30:9000; } }
6.2 Scenario 2: Path-based forwarding
Will visit/api
The request for the path is forwarded tohttp://backend-server/api
, other paths return 404.
server { listen 8080; server_name _; location /api/ { proxy_pass http://backend-server/api/; } location / { return 404; } }
Through this document, technical colleagues can understand how to configure Nginx for request forwarding and be familiar with the uses of several key configuration items. If you have more questions, it is recommended to refer to the official documentation or contact experienced colleagues to discuss.
This is the article about the Nginx request forwarding configuration guide. For more information about Nginx request forwarding, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!