SoFunction
Updated on 2025-03-03

nginx proxy forwarding configuration summary

Nginx configuration proxy forwarding is a common requirement for forwarding client requests to backend servers. The following are the configuration steps for Nginx proxy forwarding, including detailed operation steps and precautions:

1. Determine the location of Nginx installation and configuration file

First, make sure Nginx is installed correctly on the server. The main configuration file of Nginx is usually located in/etc/nginx/, but may also be located in other locations, such as/usr/local/nginx/conf/. In addition, Nginx supportsPassedincludeDirectives contain other configuration files, so the actual configuration may be scattered across multiple files.

2. Edit Nginx configuration file

Open the Nginx configuration file for editing. You can use a text editor (such as vim, nano, etc.) to edit files.

sudo vim /etc/nginx/
# orsudo nano /etc/nginx/

3. Configure proxy forwarding

In the Nginx configuration file, you need toserverAdd or modify in blockslocationBlock to configure proxy forwarding.locationBlocks are used to match requested URIs,proxy_passDirectives are used to specify the backend server address to which the request should be forwarded.

Here is a simple configuration example:

http {
    ...
    server {
        listen 80;
        server_name ;

        location / {
            # Forward all requests to            proxy_pass ;

            # Optional: Set request header information so that the backend server can obtain the client's real IP and other information            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            # Other agent-related configurations...        }

        # You can configure different proxy forwarding rules for different URI paths        location /api/ {
            proxy_pass ;
            # Set request header information...        }

        ...
    }
    ...
}

4. Save the configuration file and restart Nginx

After the configuration is complete, save and close the Nginx configuration file. Then, you need to restart the Nginx service to make the configuration take effect. You can restart Nginx using the following command:

sudo systemctl restart nginx
# orsudo nginx -s reload

5. Verify the configuration

After restarting Nginx, you can verify that the configuration is correct by accessing the Nginx server. If configured correctly, your request should be forwarded to the specified backend server and the corresponding response should be returned.

Things to note

  • Before configuring proxy forwarding, make sure the backend server is running properly and the correct port is listened to.
  • Nginx configurationproxy_passThe directive can be followed by the name of the URL (including protocols and ports) or the upstream server group (upstream). If you follow the URL, Nginx will append the requested URI to the URL (unless the URI is specified in the URL).
  • useproxy_set_headerInstructions can customize request header information, which is very useful for passing information such as the client's real IP address to the backend server.
  • Nginx's configuration file support includes other configuration files, which helps keep the configuration neat and modular. You can place the proxy forwarding configuration in a separate file and pass it in the main configuration fileincludeInstructions are introduced.

This is the article about nginx proxy forwarding configuration summary. For more related nginx proxy forwarding content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!