首頁  >  文章  >  運維  >  Centos7怎麼實現nginx反向代理

Centos7怎麼實現nginx反向代理

WBOY
WBOY轉載
2023-06-02 20:16:011082瀏覽

反向代理伺服器架設在伺服器端,透過緩衝經常被要求的頁面來緩解伺服器的工作量,將用戶端請求轉送給內部網路上的目標伺服器;並將從伺服器上得到的結果傳回給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反向代理 發現結果輸出為兩台伺服器輪流輸出

#

以上是Centos7怎麼實現nginx反向代理的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除