[Nginx configuration] nginx performs non-80 port forwarding, nginx80

Source: Internet
Author: User

[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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.