[Nginx configuration] nginx performs non-80 port forwarding, nginx80
One scenario
Recently, I used PHP to rewrite a project written in JAVA. To view the previous project, I need to build a local Tomcat project to run JAVA. After the configuration is successful, because the Tomcat listening port is 8080, the URL prefix islocalhost:8080When accessing a project, you must first enter this string of content, which is complicated.localhostIn this case, port 80 is accessed. To achieve this, port forwarding is required. I have little knowledge about it. For now, I want to use Nginx/Apache and other programs for forwarding. If you have a better solution, please feel free to advise.
Add Nginx Virtual Host
To forward Nginx, you must configure Nginx. You can enhance the Nginx function by adding virtual host configurations. First look at the Nginx configuration file. The author's Nginx file is in/etc/nginx/nginx.conf. We can see that Nginx has introducedvhosts.dDirectory. In this case/etc/nginx/vhosts.dCreate a file with the. conf suffix under the directory (if the directory does not exist, you need to create it yourself ).
Nginx performs non-80 port forwarding
For forwarding, you can use Nginxproxy_passConfiguration item. Nginx listens to 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 80; location / { proxy_pass http://127.0.0.1:8080; }}
Yes, that's all you need. This is the core of configuring port forwarding.
However, when you need to obtain the real IP address, you also need to add the real IP Address Configuration:
server { server_name www.test.com listen 80; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host:80; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}
proxy_set_headerThis configuration changes the http request header. The Host is the requested Host name,X-Real-IPIs the real IP address of the request,X-Forwarded-ForIndicates who initiated the request.
Summary
This configuration may be very simple for most people, but I just got started with Nginx configuration, so I recorded it and shared it with people in need. If you have any suggestions or criticisms, please note that. Through this study, we found that Nginx configuration is worth learning.