十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
下文給大家?guī)韓ginx反向代理概述及部署,希望能夠給大家在實(shí)際運(yùn)用中帶來一定的幫助,負(fù)載均衡涉及的東西比較多,理論也不多,網(wǎng)上有很多書籍,今天我們就用創(chuàng)新互聯(lián)在行業(yè)內(nèi)累計(jì)的經(jīng)驗(yàn)來做一個(gè)解答。
目前成都創(chuàng)新互聯(lián)公司已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、咸豐網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
反向代理(Reverse Proxy)方式是指以代理云服務(wù)器來接受internet上的連接請(qǐng)求,然后將請(qǐng)求轉(zhuǎn)發(fā)給內(nèi)部網(wǎng)絡(luò)上的服務(wù)器,并將從服務(wù)器上得到的結(jié)果返回給internet上請(qǐng)求連接的客戶端,此時(shí)代理服務(wù)器對(duì)外就表現(xiàn)為一個(gè)反向代理服務(wù)器。
環(huán)境準(zhǔn)備:
主機(jī)名 | IP地址 | 角色 | 系統(tǒng) |
---|---|---|---|
web-node1.com | eth0:192.168.90.201 | web-node1節(jié)點(diǎn) | CentOS7.2 |
web-node2.com | eth0:192.168.90.202 | web-node2節(jié)點(diǎn) | CentOS7.2 |
lb-node1.com | eth0:192.168.90.203 | Nginx反向代理 | CentOS7.2 |
在兩臺(tái)web-node節(jié)點(diǎn)中均使用Yum安裝一個(gè)Apache用于做真實(shí)機(jī),監(jiān)聽8080端口
web-node1.com部署
[root@web-node1 ~]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm [root@web-node1 ~]# yum install -y gcc glibc gcc-c++ make screen tree lrzsz ##部署web-node1 httpd服務(wù) [root@web-node1 ~]# yum install -y httpd [root@web-node1 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf [root@web-node1 ~]# systemctl start httpd [root@web-node1 ~]# echo "web-node1.com" > /var/www/html/index.html [root@web-node1 ~]# curl http://192.168.90.201:8080/ web-node1.com
web-node2.com部署
[root@web-node1 ~]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm [root@web-node1 ~]# yum install -y gcc glibc gcc-c++ make screen tree lrzsz ##部署web-node2 httpd服務(wù) [root@web-node1 ~]# yum install -y httpd [root@web-node1 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf [root@web-node1 ~]# systemctl start httpd [root@web-node1 ~]# echo "web-node2.com" > /var/www/html/index.html [root@web-node1 ~]# curl http://192.168.90.202:8080/ web-node2.com
Nginx 源碼編譯安裝,使其支持4層,并監(jiān)聽80端口
[root@lb-node1~]# useradd-s/sbin/nologin-M www
[root@lb-node1~]# cd/usr/local/src/
[root@lb-node1 src]# wget http://nginx.org/download/nginx-1.10.2.tar.gz
[root@lb-node1 src]# tar xf nginx-1.10.2.tar.gz
[root@lb-node1 src]# cd nginx-1.10.2
[root@lb-node1 nginx-1.10.2]#./configure--prefix=/usr/local/nginx-1.10.2 \
--user=www--group=www--with-http_ssl_module \
--with-http_stub_status_module--with-file-aio--with-stream
[root@lb-node1 nginx-1.10.2]# make&& make install
[root@web-node1~]# ln-s/usr/local/nginx-1.10.2/ /usr/local/nginx
## 測(cè)試配置并啟動(dòng)Nginx
[root@lb-node1~]#/usr/local/nginx/sbin/nginx-t
nginx: the configuration file/usr/local/nginx-1.10.2/conf/nginx.conf syntaxis ok
nginx: configuration file/usr/local/nginx-1.10.2/conf/nginx.conf testis successful
[root@lb-node1~]#/usr/local/nginx/sbin/nginx
##http段配置
upstream web-cluster{
# ip_hash;
server192.168.90.201:8080 weight=1 max_fails=3 fail_timeout=3;
server192.168.90.202:8080 weight=1 max_fails=3 fail_timeout=3;
}
server{
listen80;
server_name192.168.90.203;
location/{
proxy_pass http://web-cluster;
include proxy.conf;
}
}
測(cè)試代理
[root@lb-node1~]# curl http://192.168.90.203/
web-node1.com
[root@lb-node1~]# curl http://192.168.90.203/
web-node2.com
[root@lb-node1~]# curl http://192.168.90.203/
web-node1.com
[root@lb-node1~]# curl http://192.168.90.203/
web-node2.com
2.通過分組方式,以及User-agent實(shí)現(xiàn)不同代理
#http段配置
upstreamstatic-cluster{
server192.168.90.201:8080 weight=1 max_fails=3 fail_timeout=3;
}
upstreamdynamic-cluster{
server192.168.90.202:8080 weight=1 max_fails=3 fail_timeout=3;
}
upstreamdefault-cluster{
server192.168.90.202:8080 weight=1 max_fails=3 fail_timeout=3;
}
#需要配置本地host解析測(cè)試 server { listen 80; server_name nginx.jiege.com; location / { if ($http_user_agent ~* "Firefox"){ proxy_pass http://static-cluster; } if ($http_user_agent ~* "Chrome") { proxy_pass http://dynamic-cluster; } proxy_pass http://default-cluster; } }
測(cè)試分組
##默認(rèn)瀏覽器交給default處理[root@lb-node1 ~]# curl http://nginx.jiege.com web-node2.com 火狐瀏覽器交給static-cluster處理 谷歌瀏覽器交給dynamic-cluster處理 配置ssh以及msql反向代理
stream { upstream ssh_proxy { hash $remote_addr consistent; server 192.168.90.201:22; } upstream MySQL_proxy { hash $remote_addr consistent; server 192.168.90.202:3306; } server { listen 2222; proxy_connect_timeout 1s; proxy_timeout 300s; proxy_pass ssh_proxy; } server { listen 3333; proxy_connect_timeout 1s; proxy_timeout 300s; proxy_pass mysql_proxy; } }
2222端口代理至于node1的SSH、3333端口代理至于node2的MYSQL
## 測(cè)試連接ssh
[root@lb-node1~]# ssh-p2222 root@192.168.90.203
root@192.168.90.203's password:
Last login: Wed Oct 19 11:53:04 2016 from 192.168.80.143
[root@web-node1 ~]#
## 測(cè)試連接mysql
[root@lb-node1 ~]# mysql -h392.168.90.203 -uroot -p1 -P3333
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 273
Server version: 5.5.47-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
看了以上關(guān)于nginx反向代理概述及部署,如果大家還有什么地方需要了解的可以在創(chuàng)新互聯(lián)行業(yè)資訊里查找自己感興趣的或者找我們的專業(yè)技術(shù)工程師解答的,創(chuàng)新互聯(lián)技術(shù)工程師在行業(yè)內(nèi)擁有十幾年的經(jīng)驗(yàn)了。