" name="sm-site-verification"/>
侧边栏壁纸
博主头像
PySuper博主等级

千里之行,始于足下

  • 累计撰写 203 篇文章
  • 累计创建 14 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

Nginx 正向代理、反向代理、负载均衡

PySuper
2020-06-23 / 0 评论 / 0 点赞 / 4 阅读 / 4020 字
温馨提示:
所有牛逼的人都有一段苦逼的岁月。 但是你只要像SB一样去坚持,终将牛逼!!! ✊✊✊

Nginx 正向代理

docker
# vim /etc/nginx/conf.d/site.conf

server {
    listen 8000;
    charset utf-8;

    location /static {
        alias /home/zheng/Documents/Project/INFO/static/;
    }

    location / {
        uwsgi_max_temp_file_size 0;
        uwsgi_pass 0.0.0.0:9000;
        include /etc/nginx/uwsgi_params;
    }
}

# nginx -s reload

Nginx 反向代理

docker
### 代理单个服务器 ```nginx # vim /etc/nginx/conf.d/site.conf

server {
listen 80;
charset utf-8;
client_max_body_size 75M;

location /static {    # 配置静态文件访问
    alias /home/zheng/djangotest/static;
}

location / {    # 单个个代理服务器
    proxy_pass http://192.168.43.230:8000;
    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 X-Nginx-Proxy true;

}
}

nginx -s reload


### 代理多个服务器
```nginx
# vim /etc/nginx/conf.d/site.conf

server {
    listen 80;
    charset utf-8;
    client_max_body_size 75M;

    location /static {    # 配置静态文件访问
        alias /home/zheng/djangotest/static;
    }

    location /python {    # 第一个代理服务器
        proxy_pass http://192.168.43.230:8000;
        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 X-Nginx-Proxy true;
   }

    location /java {    # 第二个代理服务器
        proxy_pass http://192.168.43.231:8000;
        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 X-Nginx-Proxy true;
   }
}

# nginx -s reload

Nginx 负载均衡

# vim /etc/nginx/conf.d/site.conf

upstream mynginx {    # 反向代理的域名(多个--负载均衡)
    server 192.168.43.230:19000;
    server 192.168.43.231:19000;
}

server {
    listen 80;
    charset utf-8;
    client_max_body_size 75M;

    location /static {    # 配置静态文件访问
        alias /home/zheng/djangotest/static;
    }

    location / {    # 配置反向代理
        proxy_pass http://mynginx;
        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 X-Nginx-Proxy true;
   }
}

# nginx -s reload

问题

  • 用Nginx做反向代理的时候,配置静态文件
    • 把静态文件放在Nginx服务器上
    • 静态资源的访问直接通过Nginx来获取
    • 动态请求再转发到web服务器。
0

评论区