美文网首页
nginx 最简单的重定向,反向代理,正向代理配置

nginx 最简单的重定向,反向代理,正向代理配置

作者: XiWeidong | 来源:发表于2019-08-15 16:57 被阅读0次

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

02 反向代理配置(访问的www.test-from.com网页内容其实来自www.test-to.com这台服务器)

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 "*/*";
        }
}

相关文章

网友评论

      本文标题:nginx 最简单的重定向,反向代理,正向代理配置

      本文链接:https://www.haomeiwen.com/subject/rmizjctx.html