您好,登錄后才能下訂單哦!
怎么在Nginx中利用代理轉(zhuǎn)發(fā)請(qǐng)求?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
Nginx 利用代理轉(zhuǎn)發(fā)請(qǐng)求實(shí)例
nginx 作為目前最流行的web服務(wù)器之一,可以很方便地實(shí)現(xiàn)反向代理。
nginx 反向代理官方文檔: NGINX REVERSE PROXY
當(dāng)在一臺(tái)主機(jī)上部署了多個(gè)不同的web服務(wù)器,并且需要能在80和443端口同時(shí)訪問這些web服務(wù)器時(shí),可以使用 nginx 的反向代理功能,用 nginx 在80端口監(jiān)聽所有請(qǐng)求,并轉(zhuǎn)發(fā)給443端口,再由443端口統(tǒng)一依據(jù)轉(zhuǎn)發(fā)規(guī)則轉(zhuǎn)發(fā)到對(duì)應(yīng)的國(guó)內(nèi)web服務(wù)器上。
1. 國(guó)內(nèi)北京阿里云服務(wù)器(proxy.mimvp.com)
vim mimvp_proxy.conf server { listen 80; server_name proxy.mimvp.com; # 域名可以有多個(gè),用空格隔開 root www/mimvp_proxy; rewrite ^(.*)$ https://$host$1 permanent; location / { root www/mimvp_proxy; index index.php index.html index.htm; } location ~ \.php$ { root www/mimvp_proxy; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi.conf; } location ~* ^/(images|img|javascript|js|css|blog|flash|media|static)/ { root www/mimvp_proxy; expires 30d; } location ~* ^/(robots\.txt) { root www/mimvp_proxy; expires 365d; } location ~* ^/favicon\.ico { root www/mimvp_proxy; expires 365d; } location ~* ^/img/logo\.png { root www/mimvp_proxy; expires 365d; } location ~ /\.ht { deny all; } } server { listen 443 ssl http2; server_name proxy.mimvp.com; # 域名可以有多個(gè),用空格隔開 root www/mimvp_proxy; ssl on; ssl_certificate proxy.mimvp.com.crt; ssl_certificate_key proxy.mimvp.com.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; location / { root www/mimvp_proxy; index index.php index.html index.htm; } location ~ \.php$ { root www/mimvp_proxy; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi.conf; } location ~* ^/(images|img|javascript|js|css|blog|flash|media|static)/ { root www/mimvp_proxy; expires 30d; } location ~* ^/(robots\.txt) { root www/mimvp_proxy; expires 365d; } location ~* ^/favicon\.ico { root www/mimvp_proxy; expires 365d; } location ~* ^/img/logo\.png { root www/mimvp_proxy; expires 365d; } location ~ /\.ht { deny all; } }
2. 國(guó)外新加坡騰訊云服務(wù)器(proxy.mimgu.cn)
vim mimvp_proxy2.conf server { listen 80; server_name proxy.mimgu.cn; root www/mimvp_proxy; rewrite ^(.*)$ https://$host$1 permanent; ## 全部請(qǐng)求強(qiáng)制跳轉(zhuǎn)到 https } server { listen 443 ssl http2; server_name proxy.mimgu.cn; root www/mimvp_proxy; ssl on; ssl_certificate proxy.mimgu.cn.crt; ssl_certificate_key proxy.mimgu.cn.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; location / { proxy_pass https://proxy.mimvp.com; ## 全部請(qǐng)求強(qiáng)制跳轉(zhuǎn)到國(guó)內(nèi)服務(wù)器 https://proxy.mimvp.com } location ~ \.php$ { proxy_pass https://proxy.mimvp.com; } location ~* ^/(images|img|javascript|js|css|blog|flash|media|static)/ { proxy_pass https://proxy.mimvp.com; ## css,js,img 等全部要強(qiáng)制轉(zhuǎn)發(fā),否則可能排版錯(cuò)亂 } location ~* ^/(robots\.txt) { proxy_pass https://proxy.mimvp.com; } location ~* ^/favicon\.ico { proxy_pass https://proxy.mimvp.com; } location ~* ^/img/logo\.png { proxy_pass https://proxy.mimvp.com; } location ~ /\.ht { deny all; } }
3. 查看效果
http proxy.mimvp.com // 強(qiáng)制跳轉(zhuǎn)到 https proxy.mimvp.com https proxy.mimvp.com // 對(duì)外顯示的唯一域名網(wǎng)址 http proxy.mimgu.cn // 強(qiáng)制跳轉(zhuǎn)到 https proxy.mimgu.cn,然后繼續(xù)強(qiáng)制跳轉(zhuǎn)到 https proxy.mimvp.com https proxy.mimgu.cn // 強(qiáng)制跳轉(zhuǎn)到 https proxy.mimvp.com
看完上述內(nèi)容,你們掌握怎么在Nginx中利用代理轉(zhuǎn)發(fā)請(qǐng)求的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。