Nginx代理服务——正向代理

正向代理

在/opt/app/code的目录下创建一个joy.html文件

<html>
<head>
    <meta charset="utf-8">
    <title>joy1</title>
</head>
<body >
    <h1>JOY<h1>
</body>
</html>

演示,准备两台外网的主机

在183.238.199.242的主机上配置一个nginx文件admin.conf,此时本地浏览器无法访问到242这台主机

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;



 #   location ~ \.(html|ico|txt|js|css|ttf)$ {
  #          # html不缓存
  #          add_header Cache-Control no-store;
           # root /data/node/dist;
   #     }

    location / {
        if ($http_x_forwarded_for !~* "^183\.238\.199\.188"){    # 访问控制;只有定义的ip可以访问,其他ip访问返回403
             return 403;
        }
        root   /opt/app/code;
        index  index.html index.htm;
    }

使用本地浏览器访问183.238.199.242/joy.html;会返回403

在183.238.199.188这台主机上也配置一个nginx的文件zx_proxy.conf,作为代理服务器去访问83.238.199.242这台服务器

server {
    listen       80;
    server_name  localhost www.test.com;

    #charset koi8-r;
    access_log  /var/log/nginx/test_proxy.access.log  main;
    resolver 8.8.8.8;         # 配置DNS服务器
    location / {  
        proxy_pass http://$http_host$request_uri;  #域名加路径
    }  

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

使用本地服务器访问183.238.199.188这个服务器,就能请求到183.238.199.242的静态文件了

 

 

posted @ 2020-01-07 17:13  Cheney!努力才会幸运  阅读(494)  评论(0编辑  收藏  举报