Nginx作为web服务器一个重要的功能就是高性能反向代理。当然你也可以使用Nginx配置正向代理,本文是介绍如何通过宝塔主机面板的网站配置nginx的反向代理。nginx反向代理的指令不需要新增额外的模块,默认自proxy_pass指令,只需要修改配置文件就可以实现反向代理。
首先在代理的机器上加入,你要进行反代的域名↓(如图所示)
然后在返回面板,点击网站设置刚刚你要反代的域名
点击反向代理,$host是表示,任意的比如1.bt.cn,2.bt.cn,3.bt.cn都是按照域名的主机头来的,也可以指定一个域名,比如www.bt.cn,建议使用$host
开启反向代理
开启反代缓存加快静态资源访问
开启缓存后配置如下:
默认的配置有个地方是不对的,缓存清理要放在反代规则下面,放在上面是不能清理的。
修正好的配置你可以参考一下的。
- server
- {
- listen 80;
- server_name bt.cn www.bt.cn *.bt.cn;
- index index.php index.html;
- root /www/wwwroot/bt.cn;
-
- #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
- #error_page 404/404.html;
- #SSL-END
-
- #ERROR-PAGE-START 错误页配置,可以注释、删除或修改
- error_page 404 /404.html;
- error_page 502 /502.html;
- #ERROR-PAGE-END
-
- #PHP-INFO-START PHP引用配置,可以注释或修改
- #PROXY-START
- location /
- {
- proxy_pass http://125.88.182.170;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header REMOTE-HOST $remote_addr;
-
- #持久化连接相关配置
- proxy_connect_timeout 30s;
- proxy_read_timeout 86400s;
- proxy_send_timeout 30s;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade;
- proxy_set_header Connection "upgrade";
-
- add_header X-Cache $upstream_cache_status;
-
- expires max;
- }
-
- #缓存清理必须放在这里才可以用到,这个清理方式就是http://url/purge/index.html
- location ~ /purge(/.*) {
- #allow 218.85.152.11;#你自己的上网IP#
- #allow 127.0.0.1;#本地计划任务清理可用#
- #allow 0.0.0.0/24;#全部IP可以清理缓存#
- deny all;
- proxy_cache_purge cache_one cache_one $1$is_args$args;
- #access_log /www/wwwlogs/bt.cn_purge_cache.log;
- }
-
- location ~ .*\.(html|htm|txt|php|jsp|cgi|asp|aspx|flv|swf|xml)?$
- {
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header REMOTE-HOST $remote_addr;
- proxy_pass http://125.88.182.170;
-
- }
-
- location ~ .*\.(js|css|json|md|csv|log|conf|vue|jpg|jpeg|gif|png|tif|tiff|bmp|svg|psd|ico|tga|imb|mp3|mp4|avi|mpeg|rm|ra|ogg|wav|wmv|rmi|aac|rmvb|mkv|flv|swf|mov|movie|exe|ios|apk|ipa|pxl|sis|cab|deb|rar|zip|gzip|tar|7z|bzip2|dmg|gz|wim|tbz|tpz|z|jar|ttf|otf|woff|woff2|eot|sfnt)?$
- {
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header REMOTE-HOST $remote_addr;
- proxy_pass http://125.88.182.170;
-
- #缓存相关配置
- proxy_cache cache_one;
- proxy_cache_key $host$request_uri$is_args$args;
- proxy_cache_valid 200 304 301 302 1h;
-
- expires max;
- }
- #PROXY-END
- include enable-php-72.conf;
- #PHP-INFO-END
-
- #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
- include /www/server/panel/vhost/rewrite/bt.cn.conf;
- #REWRITE-END
-
- #禁止访问的文件或目录
- location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
- {
- return 444;
- }
-
- #一键申请SSL证书验证目录相关设置
- location ~ \.well-known{
- allow all;
- }
-
- access_log /www/wwwlogs/bt.cn.log;
- error_log /www/wwwlogs/bt.cn.error.log;
- }
复制代码 那么加入源站点开启了强制https的话配置如下即可- server
- {
- listen 80;
- server_name bt.cn www.bt.cn *.bt.cn;
- index index.php index.html;
- root /www/wwwroot/bt.cn;
-
- #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
- #error_page 404/404.html;
- #SSL-END
-
- #ERROR-PAGE-START 错误页配置,可以注释、删除或修改
- error_page 404 /404.html;
- error_page 502 /502.html;
- #ERROR-PAGE-END
-
- #PHP-INFO-START PHP引用配置,可以注释或修改
- #PROXY-START
- location /
- {
- proxy_pass https://125.88.182.170:443;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header REMOTE-HOST $remote_addr;
-
- #持久化连接相关配置
- proxy_connect_timeout 30s;
- proxy_read_timeout 86400s;
- proxy_send_timeout 30s;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade;
- proxy_set_header Connection "upgrade";
-
- add_header X-Cache $upstream_cache_status;
-
- expires max;
- }
-
- #缓存清理必须放在这里才可以用到,这个清理方式就是http://url/purge/index.html
- location ~ /purge(/.*) {
- #allow 218.85.152.11;#你自己的上网IP#
- #allow 127.0.0.1;#本地计划任务清理可用#
- #allow 0.0.0.0/24;#全部IP可以清理缓存#
- deny all;
- proxy_cache_purge cache_one cache_one $1$is_args$args;
- #access_log /www/wwwlogs/bt.cn_purge_cache.log;
- }
-
- location ~ .*\.(html|htm|txt|php|jsp|cgi|asp|aspx|flv|swf|xml)?$
- {
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header REMOTE-HOST $remote_addr;
- proxy_pass https://125.88.182.170:443;
-
- }
-
- location ~ .*\.(js|css|json|md|csv|log|conf|vue|jpg|jpeg|gif|png|tif|tiff|bmp|svg|psd|ico|tga|imb|mp3|mp4|avi|mpeg|rm|ra|ogg|wav|wmv|rmi|aac|rmvb|mkv|flv|swf|mov|movie|exe|ios|apk|ipa|pxl|sis|cab|deb|rar|zip|gzip|tar|7z|bzip2|dmg|gz|wim|tbz|tpz|z|jar|ttf|otf|woff|woff2|eot|sfnt)?$
- {
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header REMOTE-HOST $remote_addr;
- proxy_pass https://125.88.182.170:443;
-
- #缓存相关配置
- proxy_cache cache_one;
- proxy_cache_key $host$request_uri$is_args$args;
- proxy_cache_valid 200 304 301 302 1h;
-
- expires max;
- }
- #PROXY-END
- include enable-php-72.conf;
- #PHP-INFO-END
-
- #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
- include /www/server/panel/vhost/rewrite/bt.cn.conf;
- #REWRITE-END
-
- #禁止访问的文件或目录
- location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
- {
- return 444;
- }
-
- #一键申请SSL证书验证目录相关设置
- location ~ \.well-known{
- allow all;
- }
-
- access_log /www/wwwlogs/bt.cn.log;
- error_log /www/wwwlogs/bt.cn.error.log;
- }
复制代码
|
|