Home >php教程 >php手册 >PHP整合Nginx反向代理的用法

PHP整合Nginx反向代理的用法

WBOY
WBOYOriginal
2016-06-06 20:13:101315browse

Nginx 配置虚拟主机域名 blog.lixiphp.com,当浏览者访问主站时,通过代理去访问 server1.blog.lixiphp.com 网站。从而达到一种负载均衡的作用。 以下是Nginx负载均衡,反向代理的配置: server { server_name blog.lixiphp.com; listen 80; location / { pr

Nginx 配置虚拟主机域名 blog.lixiphp.com,当浏览者访问主站时,通过代理去访问 server1.blog.lixiphp.com 网站。从而达到一种负载均衡的作用。


Nginx负载均衡反向代理+PHP实现

以下是Nginx负载均衡,反向代理的配置:

server {
    server_name  blog.lixiphp.com;
    listen 80;
    location / {
        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 Via "nginx";
        proxy_pass http://server1.blog.lixiphp.com:80;
    }
}

通过proxy_set_header设置 Header信息,包括Host Name(主机域名),X-Real-IP(真实IP),发送者。

PHP获取代理之前的真实信息

通过打印PHP $_SERVER即可获取到代理之前的信息。

1. 获取Host Name(主机域名)

[HTTP_HOST] => blog.lixiphp.com

如果未经proxy_set_header Host $host; 设置的话,它的值为 server1.blog.lixiphp.com。

2. 获取访问者的真实IP地址

[HTTP_X_REAL_IP] => 58.220.197.34
[HTTP_X_FORWARDED_FOR] => 58.220.197.34

如果未经proxy_set_header X-Real-IP 和 X-Forwarded-For 设置的话,它的值为 server1.blog.lixiphp.com 的IP地址,这样就非常糟糕。

(...)
Read the rest of PHP整合Nginx反向代理的用法 (42 words)


© Li Xi for LixiPHP, 2014. | Permalink | No comment | Add to del.icio.us
Post tags: Host, LNMP, Nginx, 反向代理, 负载均衡

Feed enhanced by Better Feed from Ozh

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn