宝塔面板自带了反向代理设置, 通过Nginx反向代理我们可以让网站达到加速的效果,
如果开启缓存还可以减少源服务器带宽压力,现在简单说说这个设置.
- 以cs.iocc.online为例
- 若cs.iocc.online源服务器为A, 反代节点服务器为B和C.
- 假设源服务器A的IP为:
173.82.206.56
- 反代节点服务器B和C:
67.230.184.21
/47.74.192.19
首先要在反代服务器节点(B和C)上设置 hosts.
vi /etc/hosts
输入源服务器A的IP地址, 如
173.82.206.56 cs.iocc.online
- 在反代服务器节点(B和C)上宝塔面板里新建网站:
- 在宝塔网站设置内,添加反向代理:
修改反代配置文件,添加如下代码:
代码#PROXY-START/ location /user/ { expires 12h; if ($request_uri ~* "(php|jsp|cgi|asp|aspx)") { expires 0; } proxy_pass https://cs.iocc.online; 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; } location / { expires 12h; proxy_request_buffering off; if ($request_uri ~* "(php|jsp|cgi|asp|aspx)") { expires 0; } location ~/user/.*\.(php|cgi)$ { expires 0; } proxy_pass https://cs.iocc.online; 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; location ~ (/user|/user/code|/user/code_check)?$ { proxy_set_header Host cs.iocc.online; 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://cs.iocc.online; proxy_set_header Accept-Encoding ""; sub_filter_once off; proxy_request_buffering off; expires -1; } #持久化连接相关配置 #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; proxy_cache cache_one; proxy_cache_key $host$uri$is_args$args; proxy_cache_valid 200 304 301 302 1m; } #PROXY-END/
最后将域名
cs.iocc.online
解析到反代服务器B和C上,待解析生效即可。扩展:
查看缓存是否生效,查看宝塔默认缓存路径:
/www/server/nginx/proxy_cache_dir
是否有缓存文件。/www/server/nginx/proxy_cache_dir
- 关于SSL
如果源站开启了SSL,反代服务器也需要添加SSL证书,复制如下即可: 禁止搜索引擎收录
网站配置文件中添加如下代码即可:#禁止搜索引擎访问 if ($http_user_agent ~* (baiduspider|360spider|haosouspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler)) { return 403; }
进阶设置: