Ngnix reverse proxy binding domain name

Source: Internet
Author: User
Tags server website website server browser cache vps

Recently, a small station started with an Alibaba cloud server. In fact, the reason is that foreign servers are too stuck and SSH connections are stuck, it is useless, however, the foreign VPS has changed the hen, and China Telecom's outbound broadband has become normal. In this way, we have two VPS, just recently reading Nginx, I also saw a lot of articles about Ngnix proxy on the website, so I plan to experiment with it:

1. First, parse the reverse proxy schematic and give an image from the Internet:

From this figure, we can see that the proxy server is in the middle of the client and the server, so it can be used as a firewall. Now there are many VPS on the station, but there are only two idle servers, so you can use one as the website server and one as the proxy server.

2. the WEB server should be installed on the proxy server first, and apache can be used. However, Nginx is used for lightweight purposes, and LNMP scripts are used for small websites, under/usr/local/nginx/conf/, you can find the ngnix configuration file nginx. conf, in order to test, we first resolve the domain name, take a second-level sub-domain name: http://test.defel.net, in DNSPOD resolution to the proxy server:

This is only an experiment, and may expire in the future, so the specific IP address is not displayed. Set the proxy server to create a new virtual server, start vhost. sh, enter the domain name test.defel.net, and then y is basically the same:

After the virtual server is created, go to the/usr/local/ngnix/conf/vhost Directory, edit the configuration file, delete all the content, and then copy the following content and modify it as shown in the figure:

The following code replaces the server IP address with the real IP address of the underlying website server ):

The code is as follows: Copy code

Server
 {
Listen 80;
Server_name test.defel.net;
Location /{
Proxy_pass http: // server IP address: 8080;
Proxy_redirect off;
If ($ request_filename ~ *. *. (Gif | jpg | jpeg | png | bmp | swf) $)
 {
Expires 30d;
 }
If ($ request_filename ~ . *. (Js | css )? $)
 {
Expires 12 h;
 }
Proxy_set_header Host server IP address;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
Proxy_set_header Content-Type $ content_type;
# Proxy_hide_header Content-Type;
 }
 }

The destination port here is 8080, and the proxy server will use port 8080 to proxy the underlying website server. Therefore, in the configuration of the following website server, Ngnix must listen to port 8080, once the agent initiates a request and responds promptly, use

1
Nginx-t

Check whether the configuration file is incorrect. nginx: the configuration file/usr/local/nginx/conf/nginx is displayed. conf syntax is OK and nginx: configuration file/usr/local/nginx/conf/nginx. conf test is successful indicates that the modification code is correct, and then use the command

1
Kill  -Hup' Cat  /Data/nginx/logs/nginx . Pid'

Restart Ngnix smoothly.

3. set the configuration file of the website server. Here we use Alibaba cloud server. We also create a virtual server with the domain name test.defel.net like the proxy server configuration. Go to the vhost folder and change its configuration file, on this website server, you do not need to change all of them. You just need to change the listening port:

After the configuration is complete, use nginx-t to check whether the configuration file is correct. If you change the port, there should be no errors. Therefore, restart Nginx, after you can use the domain name http://test.defel.net access test, remember to first clean up the browser cache, to ensure the correctness of the resolution.

4. Access is shown in the following figure:

I uploaded a static page to the root directory of the Alibaba Cloud server website, just to look better than the simple hello world.

5. Route tracing information is as follows:

From the tracing result of the vro, we can see that the server IP address of the website cannot be queried. Secondly, the website opening speed is not completely reflected by the tracing information, after I set up the website, it was still very fast to open the website. It felt very good and the speed was good. If you like it, you can try it and start it on your own.

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.