01 重定向配置 (网页跳转)
server {
listen 80;
server_name www.test-from.com; #访问域名
rewrite "^/(.*)$" http://www.test-to.com/$1 break; #重定向地址
}
"^/(.*)$"
配置表示不包括域名的url地址,$1
表示前面括号里匹配到的地址,如url地址为: /news/hot/index.html
匹配到的 $1 是 news/hot/index.html
server {
listen 80;
server_name www.test-from.com; #访问域名
location / {
proxy_pass http://www.test-to.com; #反向代理地址
index index.html index.htm index.php; #默认首页
}
}
03 正向代理(通过这台服务器9999端口访问其他网站)
server {
access_log /var/log/nginx/access.log; #日志文件
listen 9999; #代理端口,通过此端口发请求转发出去
location / {
set $agent "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"; #定义一个User-Agent头信息变量$agent
resolver 8.8.8.8; #DNS服务器地址,这里用的google的
proxy_pass $scheme://$http_host$request_uri; # 转发请求
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_set_header User-Agent $agent; #设置User-Agent头信息
proxy_set_header accept-encoding "gzip";
proxy_set_header connection "keep-alive";
proxy_set_header accept "*/*";
}
}