怎么在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。