咱們米撲科技的業務遍及全球,有時國外的客戶沒法訪問咱們搭建在國內的服務,這就要求咱們在國外服務器上搭建一個代理轉發服務,用戶請求國外服務器的域名,而後代理轉發到國內,處理請求後返回結果給國外的客戶。Nginx 不只能夠實現負載均衡,還能夠實現反向代理轉發,很是適合咱們的使用場景,所以咱們米撲科技採用 Nginx 利用代理轉發請求實例。
米撲博客原文:Nginx 利用代理轉發請求實例javascript
Nginx 利用代理轉發請求實例php
nginx 做爲目前最流行的web服務器之一,能夠很方便地實現反向代理。css
nginx 反向代理官方文檔: NGINX REVERSE PROXYhtml
當在一臺主機上部署了多個不一樣的web服務器,而且須要能在80和443端口同時訪問這些web服務器時,能夠使用 nginx 的反向代理功能,用 nginx 在80端口監聽全部請求,並轉發給443端口,再由443端口統一依據轉發規則轉發到對應的國內web服務器上。java
示例:nginx
proxy.mimvp.com 域名在阿里雲備案,部署在國內的北京阿里雲服務器web
proxy.mimgu.cn 域名在騰訊雲備案,部署在國外的新加坡騰訊雲服務器(國外域名可不備案)vim
1. 國內北京阿里雲服務器(proxy.mimvp.com)服務器
vim mimvp_proxy.confsession
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
小結