nginx反向代理

1、直接反向代理: location / { proxy_pass http://192.168.163.100; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }

2、假如后端两个程序一样,如何做负载均衡: upstream backend { server 192.168.163.200 max_fails=3 fail_timeout=10s; server 192.168.163.201 max_fails=3 fail_timeout=10s; }

location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }

##反向代理。 算法: Nginx的upstream目前支持5种方式的分配 1 轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除

2 weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

3 ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以 解决session的问题。

4 fair(第三方) 按后端服务器的响应时间来分配请求,响应时间短的优先分配。

5 url_hash(第三方) 按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务 器为缓存时比较有效。

每个设备的状态设置为: 1.down 表示单前的server暂时不参与负载 2.weight 默认为1.weight越大,负载的权重就越大。 3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误 4.fail_timeout:max_fails次失败后,暂停的时间。 5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

实现精通解析: location .(jpg|png|css)$ { proxy_pass http://img.xhk.com; ##(给页面跳转) }

##绑定hosts文件: 后端绑定域名: server_name img.xhk.com;

4、解析php代码: server { listen 80; server_name www.xhk.com; access_log logs/host.access.log main; location / { root /web; index index.php index.html; } location ~ .php$ { root /web; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }

1、worker_processes 8;

2、绑定CPU:

3、文件打开数量: worker_rlimit_nofile 102400;

4、指定IO模型: use epoll;

5、连接数调整: worker_connections 102400;

6、keepalived:长链接

7、头部缓冲区: client_header_buffer_size 4k;

8、打开文件缓存:

9、打开sendfile: Senile on

10、开启压缩: gzip. On

11、静态文件缓存: Cache.

12、动态缓存: faascgi

13、php代码缓存加速: xcacahe (在编译的时候直接仅以进去,opcache)