工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆
浏览模式: 标准 | 列表分类:nginx

Nginx如何保留真实IP和获取前端IP

2,apache配置:
用mod_rpaf来获取IP的
所以需要安装这个模块
下载:http://stderr.net/apache/rpaf/download/
tar zxvf mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6
/usr/local/www/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
 
配置apache:
在 httpd.conf中添加
LoadModule rpaf_module libexec/apache2/mod_rpaf-2.0.so
RPAFenable On
RPAFsethostname On
RPAFproxy_ips 192.168.1.1 #这个是前段的IP,可不是后端的IP哦
RPAFheader X-Forwarded-For
 

nginx中配置跨域支持功能

在nginx.conf中配置
http {
  ......
  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Headers X-Requested-With;
  add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
  ......
}
这样就可以实现GET,POST,OPTIONS的跨域请求的支持
也可以 add_header Access-Control-Allow-Origin http://test.51testing.com; --指定允许的url;

nginx负载均衡基于ip_hash的session粘帖

nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个C类地址段中的客户端选择同一个后端服务器,除非那个后端服务器宕了才会换一个。

nginx的upstream目前支持的5种方式的分配


1、轮询(默认) 
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 
upstream backserver { 
server 192.168.0.14; 
server 192.168.0.15; 
} 

2、指定权重 
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 
upstream backserver { 
server 192.168.0.14 weight=10; 
server 192.168.0.15 weight=10; 
} 

3、IP绑定 ip_hash 
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 
upstream backserver { 
ip_hash; 
server 192.168.0.14:88; 
server 192.168.0.15:80; 
} 

4、fair(第三方) 
按后端服务器的响应时间来分配请求,响应时间短的优先分配。 
upstream backserver { 
server server1; 
server server2; 
fair; 
} 

5、url_hash(第三方) 
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 
upstream backserver { 
server squid1:3128; 
server squid2:3128; 
hash $request_uri; 
hash_method crc32; 
} 

在需要使用负载均衡的server中增加 

proxy_pass http://backserver/; 
upstream backserver{ 

ip_hash; 
server 127.0.0.1:9090 down; (down 表示单前的server暂时不参与负载) 
server 127.0.0.1:8080 weight=2; (weight 默认为1.weight越大,负载的权重就越大) 
server 127.0.0.1:6060; 
server 127.0.0.1:7070 backup; (其它所有的非backup机器down或者忙的时候,请求backup机器) 
} 

max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误 
  

fail_timeout:max_fails次失败后,暂停的时间

文章转自:http://www.niaox.cn/post/nginx_ip_hash_session.html

nginx_upstream_hash 增加一致性hash

url hash是用于提高squid命中率的一种架构算法

nginx_upstream_hash 介绍
Nginx_upstream_hash 是nginx 的一个第三方模块,支持采用nginx 内部的各种变
量作hash,然后针对生成的hash 值,用求余的方式分布到后端( backend)服务器上,
达到负载均衡的目的。就是说每个后端服务器只保存一份cache,不会造成cache 空间的
浪费。

为什么需要在nginx_upstream_hash 上增加一致性hash 功能?
因为nginx_upstream_hash 内部的算法采用hash 求余的方式选择后端的服务器,当
你要增加服务器的时候,整个服务器群的cache 都会受到影响,产生瞬间的后端负载,对
业务造成影响。通过增加一致性hash 功能,只影响部分后端服务器。保证了业务的平稳运
行。对系统的扩展带来了便利。

具体的操作方法
1、补丁下载地址:
https://bbs.be10.com/code/upstream_hash/nginx.path

https://bbs.be10.com/code/upstream_hash/upstream_hash.path

#为nginx 打补丁
cd nginx-0.7.17
patch -p1 < ../nginx.path
#为ngixn_upstream_hash 打补丁
cd nginx_upstream_hash-0.3
patch -p1 < ../upstream_hash.path
2、为系统增加ketama 的md5 库
下载ketama-0.1.1.tar.bz2
cd ketama/libketama
gcc -fPIC -O3 -c md5.c
gcc -shared -o libmd5.so md5.o
cp libmd5.so /usr/local/lib
cp md5.h /usr/local/include
编辑/etc/ld.so.conf
添加一行/usr/local/lib
运行ldconfig
为系统增加用户自定义的动态库路径,要不nginx 运行的时候可能报错

nginx报错”could not build the server_names_hash”

今天在给nginx添加几十个域名后,重启nginx的时候,报错”[emerg]: could not build the server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 128″

在nginx.conf配置文件的http{}把server_names_hash_bucket_size 128改为server_names_hash_bucket_size 512,按32的倍数往上加,再重启问题解决。

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t    [emerg]: could not build the server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 128 configuration file /usr/local/nginx/conf/nginx.conf test failed

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t    the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok configuration file /usr/local/nginx/conf/nginx.conf test is successful

最后查了下资料:

保存服务器名字的hash表是由指令 server_names_hash_max_size 和 server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果 hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键值。因此,如果Nginx给出需要增大 hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.

Nginx 多核cpu 优化:(Core) + worker_processes (worker_cpu_affinity)

  1. 配置14 CPU (4 Core) + 4 worker_processes (每个worker_processes 使用1个CPU)   
  2. [reistlin@reistlin.com ~]$ cat /proc/cpuinfo | grep processor   
  3. processor       : 0  
  4. processor       : 1  
  5. processor       : 2  
  6. processor       : 3  
  7. worker_processes 4;   
  8. worker_cpu_affinity 0001 0010 0100 1000;   
  9. 配置28 CPU (8 Core) + 8 worker_processes (每个worker_processes 使用1个CPU)   
  10. [reistlin@reistlin.com ~]$ cat /proc/cpuinfo | grep processor   
  11. processor       : 0  
  12. processor       : 1  
  13. processor       : 2  
  14. processor       : 3  
  15. processor       : 4  
  16. processor       : 5  
  17. processor       : 6  
  18. processor       : 7  
  19. worker_processes 8;   
  20. worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;   
  21. 配置316 CPU (16 Core) + 16 worker_processes (每个worker_processes 使用1个CPU)   
  22. [reistlin@reistlin.com ~]$ cat /proc/cpuinfo | grep processor   
  23. processor       : 0  
  24. processor       : 1  
  25. processor       : 2  
  26. processor       : 3  
  27. processor       : 4  
  28. processor       : 5  
  29. processor       : 6  
  30. processor       : 7  
  31. processor       : 8  
  32. processor       : 9  
  33. processor       : 10  
  34. processor       : 11  
  35. processor       : 12  
  36. processor       : 13  
  37. processor       : 14  
  38. processor       : 15  
  39. worker_processes 16;   
  40. worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000 

使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制

使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制

 

http://blog.codinglabs.org/articles/nginx-memc-and-srcache.html

 

http://blog.csdn.net/tianmo2010/article/details/8473996

Nginx 禁止IP访问及未绑定的域名跳转

今天要在Nginx上设置禁止通过IP访问服务器,只能通过域名访问,这样做是为了避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网,从网络上搜到以下解决方案:

Nginx的默认虚拟主机在用户通过IP访问,或者通过未设置的域名访问(比如有人把他自己的域名指向了你的ip)的时候生效

最关键的一点是,在server的设置里面添加这一行:
listen 80 default;
后面的default参数表示这个是默认虚拟主机。

这个设置非常有用。
比如别人通过ip或者未知域名访问你的网站的时候,你希望禁止显示任何有效内容,可以给他返回500.
目前国内很多机房都要求网站主关闭空主机头,防止未备案的域名指向过来造成麻烦。就可以这样设置:
server {
listen 80 default;
return 500;
}

也可以把这些流量收集起来,导入到自己的网站,只要做以下跳转设置就可以:
server {
listen 80 default;
rewrite ^(.*) http://www.linuxidc.com permanent;
}
==============================

按照如上设置后,确实不能通过IP访问服务器了,但是在应该用中出现当server_name后跟多个域名时,其中一个域名怎么都无法访问:

设置如下:
server
{
listen 80;
server_name www.linuxidc.com linuxidc.com
没更改之前,通过server_name 中的www.linuxidc.com linuxidc.com均可访问服务器,加入禁止IP访问的设置后,通过linuxidc.com无法访问服务器了,www.linuxidc.com可以访问

用 nginx -t 检测配置文件会提示warning:

[warn]: conflicting server name “linuxidc.com” on 0.0.0.0:80, ignored
the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/webserver/nginx/conf/nginx.conf test is successful

最后通过在listen 80 default;后再加server_name _;解决,形式如下:

#禁止IP访问
server
{
listen 80 default;
server_name _;
return 500;
}
或者
server {
listen 80 dufault;
server_name _;
rewrite ^(.*) http://www.linuxidc.net permanent;
}
这样,通过linuxidc.com就能访问服务器了,问题解决了,但具体原因还是不清楚。

分类:Nginx 成功分享标签:301, ipNginx 设置301重定向2010年3月2日iNginx没有评论 
第一种情况:访问A站定向到B站
server {
server_name www.linuxidc.net ;
rewrite ^(.*) http://www.linuxidc.com$1 permanent;
}
第二种情况:不是访问A站的全部重定向到指定页面
server {
server_name www.linuxidc.net;
if ($host != ‘linuxidc.net’ ) {
rewrite ^/(.*)$ http://www.linuxidc.com/$1 permanent;
}
}
如果写在第一个server段
使用IP访问时也将被重定向

Records:301234