Centos7怎么實現nginx反向代理

反向代理服務器架設在服務器端,通過緩沖經常被請求的頁面來緩解服務器的工作量,將客戶機請求轉發給內部網絡上的目標服務器;并將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器與目標主機一起對外表現為一個服務器。目前web網站使用反向代理,除了可以防止外網對內網服務器的惡性攻擊、緩存以減少服務器的壓力和訪問安全控制之外。

Centos7怎么實現nginx反向代理

實驗環境:

192.168.1.188 nginx負載均衡

192.168.1.189 web01服務器

192.168.1.190 web02服務器

軟件準備:

centos7.4 x86_64

nginx-1.6.3.tar.gz

安裝nginx軟件

安裝依賴軟件包命令集合
[root@localhost?~]#?yum?-y?install?openssl?openssl-devel?pcre?pcre-devel?gcc
安裝nginx軟件包命令集合
[root@localhost?~]#?mkdir?/app[root@localhost?~]#?cd?/app[root@localhost?~]#?wget?-q?http://nginx.org/download/nginx-1.6.3.tar.gz[root@localhost?~]#?useradd?-s?/sbin/nologin?-M[root@localhost?~]#?tar?xf?nginx-1.6.3.tar.gz[root@localhost?~]#?cd?nginx-1.6.3[root@localhost?~]#?./configure?--user=nginx?--group=nginx?--prefix=/app/nginx?--with-http_stub_status_module?--with-http_ssl_module[root@localhost?~]#?make?&&?make?install
配置文件

(以下操作在web01和web02上進行)

[root@localhost?~]#?vim?/app/nginx/conf/nginx.conf

將配置文件修改為以下內容

worker_processes?1; events?{ worker_connections?1024; } http?{ include?mime.types; default_type?application/octet-stream; sendfile?on; keepalive_timeout?65; log_format?main?'$remote_addr?-?$remote_user?[$time_local]?"$request"?''$status?$body_bytes_sent?"http_referer"?''"$http_user_agent"?"?$http_x_forwarded_for"'; server?{ listen?80; server_name?bbs.dengchuanghai.org; location?/?{ root?html/bbs; index?index.html?index.htm; } access_log?logs/access_bbs.log?main; }  } [root@localhost?~]#?mkdir?/app/nginx/html/bbs[root@localhost?~]#?echo?"192.168.1.189?bbs"?>>/app/nginx/html/bbs/index.html????????????[root@localhost?~]#?echo?"192.168.1.189?bbs.dengchuanghai.org"?>>?/etc/hosts????????????????????[root@localhost?~]#?echo?"192.168.1.190?bbs"?>>/app/nginx/html/bbs/index.html??[root@localhost?~]#?echo?"192.168.1.190?bbs.dengchuanghai.org"?>>?/etc/hosts

(分別在兩臺web服務器上輸入以上內容)

然后分別啟動nginx

[root@localhost?~]#?/app/nginx/sbin/nginx?-t?(檢查配置文件有無錯誤)[root@localhost?~]#?/app/nginx/sbin/nginx??啟動[root@localhost?~]#?ss?-tnlp?|?grep?80

使用cURL bbs.dengch Centos7怎么實現nginx反向代理 ,以下操作在nginx負載均衡器上進行

[root@localhost?~]#?vim?/app/nginx/conf/nginx.conf

更改為如下內容

worker_processes?1; events?{ worker_connections?1024; } http?{ include?mime.types; default_type?application/octet-stream; sendfile?on; keepalive_timeout?65; upstream?www_server_pools{ server?192.168.1.190:80?weight=1; server?192.168.1.189:80?weight=1; } server?{ listen?80; server_name?www.dengchuanghai.org; location?/?{ proxy_pass?http://www_server_pools; } } } [root@localhost?~]#?echo?"192.168.1.188?www.dengchuanghai,org"?>>?/etc/hosts

檢查語法

[root@localhost?~]#?/app/nginx/sbin/nginx?-t

啟動服務

[root@localhost?~]#?/app/nginx/sbin/nginx

Centos7怎么實現nginx反向代理 使用curl www.dengchuanghai.org Centos7怎么實現nginx反向代理 發現結果輸出為兩臺服務器輪流輸出

? 版權聲明
THE END
喜歡就支持一下吧
點贊13 分享