Cento7+nginx reverse proxy for multi-domain jump

Source: Internet
Author: User
Tags nginx reverse proxy

Cento7+nginx reverse proxy for multi-domain jump

Recently busy, so rarely write documents, recently there is a project group has a number of environments need to do mapping, so think of through nginx reverse proxy to achieve multi-domain address jump, so that summed up a bit, in fact, we all know the agent. The most common and often said is NAT, that is, the internal multiple address requests through the proxy to reach the outside. This is called the forward proxy, what is the reverse proxy-is the opposite of the forward proxy; So very good understanding, Nginx Reverse proxy function is very powerful, can be implemented through an Nginx proxy multiple domain names and different ports for proxy forwarding, so in the daily operation dimension is very helpful. Our demo today is to implement a different internal forwarding of multiple domains via Nginx reverse proxy. specifically see below:

Environment:

Hostname:a-s.ixmsoft.com

ip:192.168.5.21

Role:nginx Service

Hostname:b-s.ixmsoft.com

ip:192.168.5.22

Role:nginx Service

Hostname:d-s.ixmsoft.com

ip:192.168.5.20

Role:nginx Reverse Proxy

Requirements: By configuring Nginx reverse proxy to access different domain names to achieve the jump.

The Web services provided by our two in-house testing Nginx have been installed.

We need to edit the display on the default Nginx default display page:

Vim/usr/share/nginx/html/index.html
<! DOCTYPE Html>

192.168.5.21

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M01/8B/5B/wKioL1hKjoaz2NxaAABbnFvDJXY670.png "height=" 365 "/>

192.168.5.22

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/8B/5E/wKiom1hKjofDx-JwAABdgg8eDLg473.png "height=" 365 "/>

Then we start to install and configure the Nginx reverse proxy;

We first install Nginx, because it is Centos7, so we need to install Nginx warehouse.

Yum Install Http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpmyum Install Nginx

650) this.width=650; "title=" clip_image001[6] "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt = "Clip_image001[6]" src= "http://s3.51cto.com/wyfs02/M02/8B/5B/wKioL1hKjoijk9_QAADG6aHntJY442.png" height= "250"/ >

650) this.width=650; "title=" clip_image001[8] "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt = "Clip_image001[8]" src= "http://s3.51cto.com/wyfs02/M00/8B/5E/wKiom1hKjonjcenKAAEcfbhdmDU723.png" height= "427"/ >

We'd better have a backup of the nginx.conf file.

Cp/etc/nginx/nginx.conf/etc/nginx/nginx.conf.bak

650) this.width=650; "title=" clip_image001[10] "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt = "clip_image001[10]" src= "Http://s3.51cto.com/wyfs02/M01/8B/5E/wKiom1hKjory00k5AAAxmCrwKO0271.png" height= "54"/ >

Then we open the default nginx.conf file

Vim/etc/nginx/nginx.conf

You can see the contents of the default nginx.conf

user nginx;worker_processes 1;error_log /var/log/nginx/ error.log warn;pid /var/run/nginx.pid;events {worker_connections 1024;} Http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main   ' $remote _addr -  $remote _user [$time _local]  "$request"   "$status   $body _ bytes_sent  "$http _referer"   "$http _user_agent"   "$http _x_forwarded_for"; access_log /var/ log/nginx/access.log main;sendfile on; #tcp_nopush  on;keepalive_timeout 65; #gzip  on; include /etc/nginx/conf.d/*.conf; 

650) this.width=650; "title=" clip_image001[12] "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt = "clip_image001[12]" src= "http://s3.51cto.com/wyfs02/M02/8B/5E/wKiom1hKjo7g8KLDAACwiTUzScY202.png" height= "341"/ >

we see the nginx default configuration includes an option to include multiple profiles, so we can generally not modify this default configuration file;

And the default configuration file, already contains the Nginx configuration file loading directory, meaning, as long as it is under the/etc/nginx/conf.d/of all the. conf files will be loaded.

In addition, we need to note that the contents of this configuration file are in the HTTP zone.

650) this.width=650; "title=" clip_image001[14] "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt = "clip_image001[14]" src= "http://s3.51cto.com/wyfs02/M00/8B/5E/wKiom1hKjo-zCgUKAADN0VfcS0g215.png" height= "370"/ >

We view the default configuration file

650) this.width=650; "title=" clip_image001[16] "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt = "clip_image001[16]" src= "Http://s3.51cto.com/wyfs02/M01/8B/5E/wKiom1hKjpGw5Q3OAAA_lYRB3Mg221.png" height= "73"/ >

We view the default default.conf file contents

Vim default.conf

650) this.width=650; "title=" clip_image001[18] "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt = "clip_image001[18]" src= "http://s3.51cto.com/wyfs02/M00/8B/5B/wKioL1hKjpHS1UDBAAFQdp29f2E467.png" height= "455"/ >

We see that this configuration file has taken up 80 ports, we will modify the 80 port, otherwise there will be conflicts, change to 88 again.

650) this.width=650; "title=" clip_image001[20] "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt = "clip_image001[20]" src= "http://s3.51cto.com/wyfs02/M01/8B/5B/wKioL1hKjpLw2J8bAADwZ29VC4E661.png" height= "378"/ >

Next we create a new configuration file to configure the reverse proxy content, of course, we can also directly in the default configuration file modification, but we generally do not modify the default configuration.

Vim realy-proxy.conf

Then add the server address information

server {listen 80;server_name localhost;location /  {root html;index index.html index.htm;} error_page 500 502 503 504 /50x.html;location = /50x.html {root  html;}} Server{listen 80;server_name d1.ixmsoft.com;location / {proxy_redirect off;proxy_set_ header host  $host;p roxy_set_header x-real-ip  $remote _addr;proxy_set_header  x-forwarded-for  $proxy _add_x_forwarded_for;proxy_pass http://192.168.5.21:80;}} Server{listen 80;server_name d2.ixmsoft.com;location / {proxy_redirect off;proxy_set_ header host  $host;p roxy_set_header x-real-ip  $remote _addr;proxy_set_header  x-forwarded-for  $proxy _add_x_forwarded_for;proxy_pass http://192.168.5.22:80;}} 

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/8B/5E/wKiom1hKjpOyE4_fAABMwNp94Dc787.png "height=" 222 "/>

--------------------------------------------------------------------------------

We finally add a log file that logs the access log

We need to create a log directory under the/etc/nginx directory

Cd/etc/nginxmkdir logs

This folder has to have you start the Nginx user Write permission

650) this.width=650; "title=" clip_image001[22] "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px, "border=" 0 "alt=" clip_ IMAGE001[22] "src=" Http://s3.51cto.com/wyfs02/M02/8B/5B/wKioL1hKjpTxYjsYAAGyMYXYm6g590.png "height=" 254 "/>

and add it in the new profile

server {listen 80;server_name localhost;location /  {root html;index index.html index.htm;} error_page 500 502 503 504 /50x.html;location = /50x.html {root  html;}} Server{listen 80;server_name d1.ixmsoft.com;location / {proxy_redirect off;proxy_set_ header host  $host;p roxy_set_header x-real-ip  $remote _addr;proxy_set_header  x-forwarded-for  $proxy _add_x_forwarded_for;proxy_pass http://192.168.5.21:80;} Access_log logs/d1.ixmsoft.com_access.log;} Server{listen 80;server_name d2.ixmsoft.com;location / {proxy_redirect off;proxy_set_ header host  $host;p roxy_set_header x-real-ip  $remote _addr;proxy_set_header  x-forwarded-for  $proxy _add_x_forwarded_for;proxy_pass http://192.168.5.22:80;} Access_log logs/d2.ixmsoft.com_access.log;} 

And then restart the Nginx service.

Systemctl RESTATRT Nginx

Next we test the access

We visit d1.ixmsoft.com

650) this.width=650; "title=" clip_image018 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt = "clip_image018" src= "http://s3.51cto.com/wyfs02/M00/8B/5B/wKioL1hKjpXhgUrEAABJpU9S4Xk462.jpg" height= "248"/>

D2.ixmsoft.com

650) this.width=650; "title=" clip_image020 "style=" border-top:0px;border-right:0px;background-image:none; border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px, "border=" 0 "alt=" clip_ image020 "src=" http://s3.51cto.com/wyfs02/M01/8B/5B/wKioL1hKjpby1THUAABJKojaeOQ525.jpg "height=" 243 "/>

Finally we look at log in the log directory

Cd/etc/nginx/logs

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px; "border=" 0 "alt=" image "src=" http:// S3.51cto.com/wyfs02/m02/8b/5b/wkiol1hkjpfzao9gaabxoevtbvg106.png "height=" 193 "/>

We view the contents of two log files separately

Cat D1.ixmsoft.com_access.log

Cat D2.ixmsoft.com_access.log

We can see which machine is accessing the IP address that will be displayed

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;background-image:none;border-bottom:0 px;padding-top:0px;padding-left:0px;border-left:0px;margin:0px;padding-right:0px; "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/8B/5E/wKiom1hKjpjzPwNHAADlCZlb-KI947.png "height="/>

This article from "Gao Wenrong" blog, declined reprint!

Cento7+nginx Reverse proxy for multi-domain jump

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.