日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
CentOS配置Nginx反向代理具體方法

Nginx 服務(wù)器的反向代理服務(wù)是其最常用的重要功能,由反向代理服務(wù)也可以衍生出很多與此相關(guān)的 Nginx 服務(wù)器重要功能,比如后面會(huì)介紹的負(fù)載均衡。

創(chuàng)新互聯(lián)是一家朝氣蓬勃的網(wǎng)站建設(shè)公司。公司專(zhuān)注于為企業(yè)提供信息化建設(shè)解決方案。從事網(wǎng)站開(kāi)發(fā),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),網(wǎng)站模板,微信公眾號(hào)開(kāi)發(fā),軟件開(kāi)發(fā),微信平臺(tái)小程序開(kāi)發(fā),10多年建站對(duì)成都宣傳片制作等多個(gè)方面,擁有豐富的網(wǎng)站運(yùn)維經(jīng)驗(yàn)。

一、反向代理及演示環(huán)境描述

1、反向代理

在計(jì)算機(jī)網(wǎng)絡(luò)中,反向代理是一種代理服務(wù)器,代表客戶(hù)端從一個(gè)或多個(gè)服務(wù)器檢索資源。然后將這些資源返回給客戶(hù)機(jī),就像它們?cè)醋訵eb服務(wù)器本身一樣。與正向代理相反,正向代理是與其關(guān)聯(lián)的客戶(hù)端聯(lián)系任何服務(wù)器的中介,反向代理是任何客戶(hù)端與其關(guān)聯(lián)的服務(wù)器進(jìn)行聯(lián)系的中介。

有關(guān)正向代理可參考:基于CentOS 7配置Nginx正向代理

2、本演示中的幾個(gè)服務(wù)器

CentOS 配置 Nginx 反向代理CentOS 配置 Nginx 反向代理

二、常規(guī)反向代理配置

1、后端服務(wù)器配置(Apache)

后端Apache服務(wù)器主機(jī)名及IP

# hostname
 centos7-web.example.com
# more /etc/redhat-release
 CentOS Linux release 7.2.1511 (Core)
# ip addr|grep inet|grep global
 inet 172.24.8.128/24 brd 172.24.8.255 scope global eno16777728

# systemctl start httpd.service
# echo "This is a httpd test page.">/var/www/html/index.html
# curl http://localhost
 This is a httpd test page.

2、前端Nginx反向代理服務(wù)器配置

前端Nginx服務(wù)器主機(jī)名及IP

# hostname
 centos7-router

 # more /etc/redhat-release
 CentOS Linux release 7.2.1511 (Core)
 # ip addr |grep inet|grep global
 inet 172.24.8.254/24 brd 172.24.8.255 scope global eno16777728
 inet 192.168.1.175/24 brd 192.168.1.255 scope global dynamic eno33554960

Nginx版本

# nginx -V
 nginx version: nginx/1.10.2

添加一個(gè)新的配置文件用作反向代理

# vim /etc/nginx/conf.d/reverse_proxy.conf
 server {
   listen 8090;
   server_name localhost;

 location / {
   proxy_pass http://172.24.8.128; ###反向代理核心指令

   proxy_buffers 256 4k;
   proxy_max_temp_file_size 0;
   proxy_connect_timeout 30;

   proxy_cache_valid 200 302 10m;
   proxy_cache_valid 301 1h;
   proxy_cache_valid any 1m;
   }
 }

# systemctl reload nginx
# ss -nltp|grep nginx|grep 8090
LISTEN 0 128 *:8090 *:* users:(("nginx",pid=78023,fd=8),("nginx",pid=78021,fd=8))

# curl http://localhost:8090 ##基于本地測(cè)試
This is a httpd test page.

查看Apache服務(wù)器日志

# more /var/log/httpd/access_log ##請(qǐng)求IP地址為172.24.8.254,當(dāng)從其他機(jī)器請(qǐng)求時(shí)也是172.24.8.254這個(gè)IP
172.24.8.254 - - [30/Oct/2017:14:02:38 +0800] "GET / HTTP/1.0" 200 27 "-" "curl/7.29.0"

3、反向代理服務(wù)器及后端服務(wù)器日志格式設(shè)置

為Nginx服務(wù)器添加proxy_set_header指令,修改后如下

# grep proxy_set_header -B2 /etc/nginx/conf.d/reverse_proxy.conf
 location / {
   proxy_pass http://172.24.8.128;
   proxy_set_header X-Real-IP $remote_addr;
   }
# systemctl reload nginx.service

后端服務(wù)器Apache日志格式設(shè)置

# vim /etc/http/conf/httpd.conf

# LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined #注釋此行,添加下一行
   LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined #關(guān)鍵描述 {X-Real-IP}i

# ip addr|grep inet|grep global    #從1.132主機(jī)訪問(wèn)
 inet 192.168.1.244/24 brd 192.168.1.255 scope global eth0

# curl http://192.168.1.175:8090  #從1.244主機(jī)訪問(wèn)
 This is a httpd test page

#再次查看apache訪問(wèn)日志,如下,不再是代理服務(wù)器IP地址,此時(shí)顯示為1.244
 192.168.1.244 - - [30/Oct/2017:15:49:07 +0800] "GET / HTTP/1.0" 200 27 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu)  libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"

三、基于目錄匹配反向代理

后端服務(wù)器采用Nginx的配置

# more /etc/redhat-release ##os平臺(tái)及ip地址
 CentOS release 6.7 (Final)
# ip addr|grep eth0|grep global
 inet 192.168.1.132/24 brd 192.168.1.255 scope global eth0
# nginx -v ##nginx版本
 nginx version: nginx/1.10.2

# mkdir -pv /usr/share/nginx/html/images ##創(chuàng)建圖片目錄
 mkdir: created directory `/usr/share/nginx/html/images' # cp /usr/share/backgrounds/nature/*.jpg /usr/share/nginx/html/images/. ##復(fù)制圖片文件 # cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bk # vim /etc/nginx/conf.d/default.conf ##此處直接修改缺省配置文件  server {  listen 80 default_server;  listen [::]:80 default_server;  server_name _;  root /usr/share/nginx/html; # Load configuration files for the default server block.  include /etc/nginx/default.d/*.conf;  location / {    }  location /images {    alias /usr/share/nginx/html/images; ##此處配置了別名    }  error_page 404 /404.html;  location = /40x.html {    }  error_page 500 502 503 504 /50x.html;  location = /50x.html {    }  } # /etc/init.d/nginx reload Reloading nginx: [ OK ] 

前端Nginx配置

# vim /etc/nginx/conf.d/reverse_proxy.conf server { listen 8090; server_name localhost; location / {   proxy_pass http://172.24.8.128;   proxy_set_header X-Real-IP $remote_addr;   } location /images { ##將images目錄下的文件代理至192.168.1.132   proxy_pass http://192.168.1.132;   proxy_set_header X-Real-IP $remote_addr;   } }# systemctl reload nginx

驗(yàn)證代理情況,在ip為192.168.1.244測(cè)試對(duì)images目錄下的jpg文件請(qǐng)求

# ip addr|grep inet|grep global inet 192.168.1.244/24 brd 192.168.1.255 scope global eth0# curl -I http://192.168.1.175:8090/images/Garden.jpg HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, 31 Oct 2017 01:48:18 GMT Content-Type: image/jpeg Content-Length: 264831 Connection: keep-alive Last-Modified: Mon, 30 Oct 2017 08:21:28 GMT ETag: "59f6e108-40a7f" Accept-Ranges: bytes

四、基于特定文件類(lèi)型的反向代理配置

php服務(wù)器端配置(ip 192.168.1.132)

# ss -nltp|grep php LISTEN 0 128 192.168.1.132:9000 *:* users:(("php-fpm",7147,8),("php-fpm",7148,0),("php-fpm",7149,0))# mkdir -pv /data ###存放php代碼# echo "/data 192.168.1.0/24(rw)" >/etc/exports# /etc/init.d/rpcbind start# /etc/init.d/nfslock start# /etc/init.d/nfs start # echo "" > /data/index.php

Nginx代理端配置(ip 192.168.1.175)

# mkdir /data# mount -t nfs 192.168.1.132:/data /data# ls /data index.php# vim /etc/nginx/conf.d/reverse_proxy.conf server { listen 8090; server_name localhost; location / {   proxy_pass http://172.24.8.128;   proxy_set_header X-Real-IP $remote_addr;   } location /images {   proxy_pass http://192.168.1.132;   proxy_set_header X-Real-IP $remote_addr;   } location ~ \.php$ {   root /data;   fastcgi_pass 192.168.1.132:9000;   fastcgi_index index.php;   fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;   include fastcgi_params;   } }# systemctl restart nginx

測(cè)試反向代理至php

[root@ydq05 ~]# ip addr|grep inet|grep global inet 192.168.1.244/24 brd 192.168.1.255 scope global eth0 [root@ydq05 ~]# curl -I http://192.168.1.175:8090/index.php HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, 31 Oct 2017 03:22:59 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.6.0

五、基于upstream 配置反向代理至tomcat

Nginx upstream指令也可以將請(qǐng)求代理到后端服務(wù)器 如下示例,結(jié)合upstream指令演示將其代理到tomcat

# vim /etc/nginx/conf.d/tomcat.conf
upstream app {
               server localhost:8080;
               keepalive 32;
}

server {
   listen 80;
   server_name localhost;
   location / {
       proxy_set_header Host $host;
       proxy_set_header x-for $remote_addr;
       proxy_set_header x-server $host;
       proxy_set_header x-agent $http_user_agent;
       proxy_pass http://app;
   }
}

[root@node132 conf.d]# ss -nltp|grep java
LISTEN    0  1    ::ffff:127.0.0.1:8005  :::*      users:(("java",39559,45))
LISTEN    0  100                :::8009  :::*      users:(("java",39559,43))
LISTEN    0  100                :::8080  :::*      users:(("java",39559,42))

tomcat版本
[root@node132 conf.d]# /usr/local/tomcat/bin/catalina.sh version
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
           ....
Server version: Apache Tomcat/7.0.69
Server built:  Apr 11 2016 07:57:09 UTC
Server number:  7.0.69.0
OS Name:        Linux
OS Version:    2.6.32-573.el6.x86_64
Architecture:  amd64
JVM Version:    1.7.0_79-b15
JVM Vendor:    Oracle Corporation

驗(yàn)證結(jié)果
# curl http://localhost


       
       
       
   
   ......

六、proxy模塊指令描述

proxy模塊的可用配置指令非常多,它們分別用于定義proxy模塊工作時(shí)的諸多屬性,如連接超時(shí)時(shí)長(zhǎng)、代理時(shí)使用http協(xié)議版本等。下面對(duì)常用的指令做一個(gè)簡(jiǎn)單說(shuō)明。

 

proxy_read_timeout ?? 在連接斷開(kāi)之前兩次從接收upstream server接收讀操作的最大間隔時(shí)長(zhǎng);

如下面的一個(gè)示例:

proxy_redirect off;
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   client_max_body_size 10m;
   client_body_buffer_size 128k;
   proxy_connect_timeout 30;
   proxy_send_timeout 15;
   proxy_read_timeout 15;

文章題目:CentOS配置Nginx反向代理具體方法
文章路徑:http://www.5511xx.com/article/coidcpc.html