Nginx can be easily configured as a reverse proxy server:
server {
listen;
server_name localhost;
Location/{
Proxy_pass http://x.x.x.x:9500;
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 Via "Nginx";
}
}
However, if the Nginx listening port is not the default 80 port, change to a different port such as 81 port.
Request.getserverport () in the back-end server could not get the correct port, the return is still 80;
At Response.sendredirect (), the client may not be able to obtain the correct redirection URL.
The correct configuration method below we see in detail:
Add Nginx Virtual Host
To do nginx forwarding, of course, to Nginx to do the configuration. You can enhance the functionality of Nginx by adding a virtual host configuration. First look at the Nginx configuration file, the author of the Nginx file is in/etc/nginx/nginx.conf. From the image above you can see that nginx introduced the configuration file in the VHOSTS.D directory at the end. Then create a file with the. conf suffix in the/ETC/NGINX/VHOSTS.D directory (if the directory does not exist to create it yourself).
Nginx do not 80 port forwarding
to do forwarding, you can use the Nginx proxy_pass configuration entry. Nginx listens on port 80 and forwards the request to the URL to be forwarded. The specific configuration is as follows:
server {
server_name www.test.com
listen;
Location/{
proxy_pass http://127.0.0.1:8080;
}
}
Yes, that's the simple way to go. This is the core of configuring port forwarding.
However, when you encounter a business that needs to acquire real IP, you also need to add a configuration about real IP:
server {
server_name www.test.com
listen;
Location/{
Proxy_pass http://127.0.0.1:8080;
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 This configuration is to change the HTTP request header. The host is the requested hostname, and X-REAL-IP is the real ip,x-forwarded-for of the request to indicate who initiated the request.
Summary
This configuration may be very simple for most people, but the author has just contacted the Nginx configuration, so record it and share it with people in need. If there are suggestions and criticisms, please point out. Through this study found that the configuration of Nginx is worth learning.