您好,登錄后才能下訂單哦!
怎么在Nginx中利用代理轉發請求?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
Nginx 利用代理轉發請求實例
nginx 作為目前最流行的web服務器之一,可以很方便地實現反向代理。
nginx 反向代理官方文檔: NGINX REVERSE PROXY
當在一臺主機上部署了多個不同的web服務器,并且需要能在80和443端口同時訪問這些web服務器時,可以使用 nginx 的反向代理功能,用 nginx 在80端口監聽所有請求,并轉發給443端口,再由443端口統一依據轉發規則轉發到對應的國內web服務器上。
1. 國內北京阿里云服務器(proxy.mimvp.com)
vim mimvp_proxy.conf server { listen 80; server_name proxy.mimvp.com; # 域名可以有多個,用空格隔開 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; # 域名可以有多個,用空格隔開 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. 國外新加坡騰訊云服務器(proxy.mimgu.cn)
vim mimvp_proxy2.conf server { listen 80; server_name proxy.mimgu.cn; root www/mimvp_proxy; rewrite ^(.*)$ https://$host$1 permanent; ## 全部請求強制跳轉到 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; ## 全部請求強制跳轉到國內服務器 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 等全部要強制轉發,否則可能排版錯亂 } 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 // 強制跳轉到 https proxy.mimvp.com https proxy.mimvp.com // 對外顯示的唯一域名網址 http proxy.mimgu.cn // 強制跳轉到 https proxy.mimgu.cn,然后繼續強制跳轉到 https proxy.mimvp.com https proxy.mimgu.cn // 強制跳轉到 https proxy.mimvp.com
看完上述內容,你們掌握怎么在Nginx中利用代理轉發請求的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。