34

Nginx反向代理之动静分离

 4 years ago
source link: https://www.tuicool.com/articles/mMjQfm7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

我们已经知道了什么是正向代理与反向代理,这次我们就讲一下Nginx的动静分离的案例,其实质运用的就是反向代理,专门用一台服务器代理服务器上的图片资源。

想使用代理必然要配置代理,配置反向代理,必须要用到 proxy_pass 命令来配置。

打开nginx的配置文件nginx.conf,在你的server虚拟主机段中添加如下配置:

location ~ \.(jpg|gif|png)$ {
    proxy_pass IP:port;
}

示例:

location ~ \.(jpg|gif|png)$ {
    #         协议://IP地址:端口号(默认是80)
    proxy_pass http://image.itbsl.com;
}

思考?

反向代理导致了后盾服务器接收的客户端IP为前端服务器的IP,而不是客户的真正IP,怎么办?

答: 代理服务器通过设置头信息字段,把用户IP传到后台服务器去。

location ~ \.(jpg|gif|png)$ {
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_pass http://image.itbsl.com;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK