Nginx代理服务——反向代理

Nginx可以代理的服务

正向代理,例如翻墙

反向代理

正向和反向代理的区别

区别在于代理的对象不一样

正向代理:代理的对象是客户端

反向代理:代理的对象是服务器

配置语法

Syntax:proxy_pass URL;

Default:——

Context:location,if in location,limit_except

反向代理演示

在/opt/app/code2的目录下面创建一个html文件

<html>
<head>
    <meta charset="utf-8">
    <title>imooc1</title>
</head>
<body style="">
    <h1>Welcome to www.test.joy.com</h1>
</body>
</html>

增加2个配置文件

realserver.conf

server {
    listen       8080;                # 使用8080的端口。此端口外网无法访问
    server_name  localhost www.test.com;

    #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 / {
        root   /opt/app/code2;    #访问的静态文件为此目录的test_proxy.html
        random_index on;
        index  index.html index.htm;
    }

下方省略....

fx_proxy.conf

server {
    listen       80;            # 80端口外网是可以访问的
    server_name  www.test.ab.com;

    #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 / {
        root   /opt/app/code;
        random_index on;
        #index  index.html index.htm;
    }

    location ~ /test_proxy.html$ {
        proxy_pass http://127.0.0.1:8080;   #通过代理访问8080端口
    }

配置好文件后,重启nginx -s reload

此时我们用外网通过8080的端口去访问,无法访问到。

使用80端口访问,能正常访问了

原理:在服务器本地8080端口无法访问,通过80端口作为代理去访问8080的服务

猜你喜欢

转载自www.cnblogs.com/joy-sir/p/12162573.html