Nginx the port forwarding method with non 80 ports configured in the server is detailed _nginx

Source: Internet
Author: User
Tags http request

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.

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.