您好,登錄后才能下訂單哦!
下文給大家?guī)砘贚inux Centos7 實(shí)現(xiàn)nginx的代理云服務(wù)器以及動(dòng)靜分離的方法和步驟,希望能夠給大家在實(shí)際運(yùn)用中帶來一定的幫助,負(fù)載均衡涉及的東西比較多,理論也不多,網(wǎng)上有很多書籍,今天我們就用億速云在行業(yè)內(nèi)累計(jì)的經(jīng)驗(yàn)來做一個(gè)解答。
一:環(huán)境
準(zhǔn)備一個(gè)nginx代理云服務(wù)器 三臺(tái)http服務(wù)器兩臺(tái)處理靜態(tài)和一臺(tái)處理動(dòng)態(tài)。(nginx/1.17.3)
二、在nginx主配置文件配置nginx反向代理upstream(地址池)指向真實(shí)服務(wù)器
vim /etc/nginx/nginx.conf
在http標(biāo)簽中加
upstream static {
server 10.30.161.214:80 weight=2 max_fails=2 fail_timeout=2s;
server 10.30.161.242:80 weight=2 max_fails=2 fail_timeout=2s;
}
upstream php {
server 10.30.161.241:80 weight=2 max_fails=2 fail_timeout=2s;
}
三、在子配置文件中
vim /etc/nginx/conf.d/proxy.conf
1、動(dòng)態(tài)資源加載
在server標(biāo)簽中添加
location ~ \.(php|jsp)$ {
proxy_pass http://phpserver;
#指向動(dòng)態(tài)服務(wù)器地址池
proxy_set_header Host $host:$server_port;
#重新定義或者添加發(fā)往后端服務(wù)器的請求頭$host真實(shí)服務(wù)器主機(jī)名$server_port端口
proxy_set_header X-Real-IP $remote_addr;
#啟用客戶端真實(shí)地址(否則日志中顯示的是代理在訪問網(wǎng)站)
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#顯示http請求端的真實(shí)IP
}
2、靜態(tài)資源加載
location ~ .*\.(html|gif|jpg|png|bmp|swf|css|js)$ {
proxy_pass http://static;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
至此代理服務(wù)器配置完成
四、兩臺(tái)靜態(tài)服務(wù)器的單獨(dú)配置
server {
listen 80;
server_name localhost;
location ~ \.(html|jpg|png|js|css|gif|bmp|jpeg) {
root /web1;
index index.html;
}
}
配置靜態(tài)項(xiàng)目
靜態(tài)服務(wù)器1
mkdir /web1
echo "this is jingtai11111" > /web1/index.html
靜態(tài)服務(wù)器2
mkdir /web1
echo "this id jingtai22222" > /web1/index.html
五、一臺(tái)動(dòng)態(tài)服務(wù)器的單獨(dú)配置
yum 安裝php7.1
[root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
[root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@nginx-server ~]#yum install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt -y
[root@nginx-server ~]#yum install -y php71w-fpm
[root@nginx-server ~]#systemctl start php-fpm
[root@nginx-server ~]#systemctl enable php-fpm
編輯nginx的配置文件:
server {
listen 80;
server_name localhost;
location ~ \.php$ {
root /web1; #指定網(wǎng)站目錄
fastcgi_pass 127.0.0.1:9000; #指定訪問地址
fastcgi_index index.php; #指定默認(rèn)文件
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #站點(diǎn)根目錄,取決于root配置項(xiàng)
include fastcgi_params; #包含nginx常量定義
}
}
配置動(dòng)態(tài)web1上項(xiàng)目
mkdir /web1
cd /web1
vim index.php
<?php
phpinfo();
?>
六、測試
1,靜態(tài)訪問測試
用瀏覽器訪問代理服務(wù)器IP 10.30.161.51/index.html
即可訪問到靜態(tài)服務(wù)器/web1上的項(xiàng)目,并且兩臺(tái)服務(wù)器來回切換,這就實(shí)現(xiàn)了nginx的代理和負(fù)載均衡
2,動(dòng)態(tài)訪問測試
用瀏覽器訪問代理服務(wù)器IP 10.30.161.51/index.php
即可訪問到動(dòng)態(tài)服務(wù)器/web1上的項(xiàng)目,從而實(shí)現(xiàn)了nginx的動(dòng)靜分離
看了以上關(guān)于基于Linux Centos7 實(shí)現(xiàn)nginx的代理服務(wù)器以及動(dòng)靜分離的方法和步驟,如果大家還有什么地方需要了解的可以在億速云行業(yè)資訊里查找自己感興趣的或者找我們的專業(yè)技術(shù)工程師解答的,億速云技術(shù)工程師在行業(yè)內(nèi)擁有十幾年的經(jīng)驗(yàn)了。億速云官網(wǎng)鏈接www.kemok4.com
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。