存档

文章标签 ‘nginx’

完美解决nginx环境下服务器上多站点WebShell访问限制问题

2011年7月6日 没有评论

完美解决nginx环境下服务器上多站点WebShell访问限制问题

http://www.discuz.net/thread-1497466-1-1.html


wget http://www.php.net/get/php-5.2.10.tar.gz/from/this/mirror
wget http://php-fpm.org/downloads/php-5.2.10-fpm-0.5.13.diff.gz
tar zxvf php-5.2.10.tar.gz
gzip -cd php-5.2.10-fpm-0.5.11.diff.gz | patch -d php-5.2.10 -p1
cd php-5.2.10/
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-ftp --without-pear


vi main/fopen_wrappers.c


/* {{{ php_check_open_basedir
*/
PHPAPI int php_check_open_basedir_ex(const char *path, int warn TSRMLS_DC)
{
/* Only check when open_basedir is available */
if (PG(open_basedir) && *PG(open_basedir)) {
char *pathbuf;
char *ptr;
char *end;

下面加上:
char *env_document_root = sapi_getenv("DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT")-1 TSRMLS_CC);
if (php_check_specific_open_basedir(ptr, path TSRMLS_CC) == 0) {
efree(env_document_root);
return 0;
}

然后保存,退出。

php.in的open_basedir配置:
修改:
;open_basedir =

open_basedir = “/tmp/:/var/tmp/”

重启
/usr/local/php/sbin/php-fpm restart

如何安全的去除Nginx服务器的版本号

2011年3月11日 没有评论

http {
……省略配置
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
server_tokens off;
…….省略配置
}
重启nginx /usr/local/webserver/nginx/bin/nginx -s reload

这是我的nginx+tomcat服务器上执行前后的对比结果
[root@localhost ~]# curl –head 127.0.0.1
HTTP/1.1 403 Forbidden
Server: nginx/0.8.46
Date: Fri, 11 Mar 2011 08:26:51 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 169
Connection: keep-alive

[root@localhost ~]# /usr/local/webserver/nginx/sbin/nginx -s reload
[root@localhost ~]# curl –head 127.0.0.1
HTTP/1.1 403 Forbidden
Server: nginx
Date: Fri, 11 Mar 2011 08:26:58 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 162
Connection: keep-alive

如果服务器跑的是nginx+php-fpm,这时候需要编辑php-fpm配置文件 如fcgi.conf、fastcgi.conf(要看你是什么配置文件名),久违的403报错,看看,有没有nginx版本泄露的信息:

fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
改为fastcgi_param SERVER_SOFTWARE nginx;

记得平滑重启下 平滑重启:
/usr/local/webserver/nginx/sbin/nginx -s reload

转自:http://hi.baidu.com/yuhongchun027/blog/item/a4b25f774db3620fb051b993.html

分类: nginx相关 标签:

由nginx漏洞想到的禁止某个目录执行php权限

2010年5月22日 没有评论

这次的nginx漏洞可谓是惊天,具体就不说了,详情见 http://www.80sec.com/nginx-securit.html
这漏洞可怕之处就是一旦有用户可以上传的地方如果能把php改后缀方式上传到服务器的话,就能任意执行。
其实有一个通用的方法可以来避免这种未知漏洞,就是禁止用户上传目录执行php权限。
在apache下面我们可以通过:

1
2
3
   <Directory /website/attachments>
        php_flag engine off
   </Directory>

的方式来来禁用目录下文件php执行权限。
那么在nginx里面同样可以实现这种方法,那就是location的优先匹配,关于location可以参考我之前的一批文章http://226617.cn/archives/392.htm
这里简单就举个例子

1
2
3
4
   location ^~ /attachments/
   {
      access_log  off;
   }

这样 attachments这个目录 就不会再去跳转给fastcgi去执行php了

分类: nginx相关 标签: , ,

Nginx+keepalived负载均衡篇

2009年11月6日 3 条评论

由于nginx的url hash功能可以很好的提升squid的性能,所以我把squid前端的负载均衡器更换为nginx,但是一台nginx就形成了单点,现在使用keepalived来解决这个问题,keepalived的故障转移时间很短(<1s),而且配置简单,这也是选择keepalived的一个主要原因,建议日PV值小的中小型企业web均可采用如下方案实行,下面直接上安装步骤:

一、环境:
centos5.3、nginx-0.7.51、keepalived-1.1.19
主nginx负载均衡器:192.168.0.154
辅nginx负载均衡器:192.168.9.155
vip:192.168.0.188

二、安装keepalived

#tar zxvf keepalived-1.1.19.tar.gz
#cd keepalived-1.1.19
#./configure --prefix=/usr/local/keepalived
#make
#make install
#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
#mkdir /etc/keepalived
#cd /etc/keepalived/
vim keepalived.conf

! Configuration File for keepalived
global_defs {
notification_email {
yuhongchun027@163.com
}
notification_email_from keepalived@chtopnet.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.0.155 <==辅nginx的IP地址
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
192.168.0.188 <==vip地址
}
}
#service keepalived start
我们来看一下日志:
[root@ltos ~]# tail /var/log/messages
Oct 6 03:25:03 ltos avahi-daemon[2306]: Registering new address record for 192.168.0.188 on eth0.
Oct 6 03:25:03 ltos avahi-daemon[2306]: Registering new address record for 192.168.0.154 on eth0.
Oct 6 03:25:03 ltos avahi-daemon[2306]: Registering HINFO record with values 'I686'/'LINUX'.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Withdrawing address record for fe80::20c:29ff:feb9:eeab on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Withdrawing address record for 192.168.0.154 on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Host name conflict, retrying with
Oct 6 03:25:23 ltos avahi-daemon[2306]: Registering new address record for fe80::20c:29ff:feb9:eeab on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Registering new address record for 192.168.0.188 on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Registering new address record for 192.168.0.154 on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Registering HINFO record with values ‘I686′/’LINUX’.

很显然vrrp已经启动,我们还可以通过命令:#ip a 来检查

[root@ltos html]# ip a
1: lo: mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:ba:9b:e7 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.154/24 brd 192.168.0.255 scope global eth0
inet 192.168.0.188/32 scope global eth0
inet6 fe80::20c:29ff:feba:9be7/64 scope link
valid_lft forever preferred_lft forever
3: sit0: mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0

说明vip已经启动,这样主服务器就配置好了,辅机的配置大致一样,除了配置文件有少部分的变化,下

面贴出辅机的配置文件:

! Configuration File for keepalived
global_defs {
notification_email {
yuhongchun027@163.com
}
notification_email_from keepalived@chtopnet.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.0.154 <==主nginx的IP的地址
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
192.168.0.188
}
}

检查其配置
[root@ltos html]# ip a
1: lo: mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:ba:9b:e7 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.155/24 brd 192.168.0.255 scope global eth0
inet 192.168.0.188/32 scope global eth0
inet6 fe80::20c:29ff:feba:9be7/64 scope link
valid_lft forever preferred_lft forever
3: sit0: mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0

测试其效果方法很简单,分别在主辅机上建立不同的主页,index.html分别为192.168.0.154,192.168.0.155,然后用客户机上elinks http://192.168.0.188,主机down掉后辅机会马上接替提供服务,间隔时间几乎无法感觉出来,这个环境准备再进行下压力测试,用于我杭州网跃朋友的web服务器,如有疑问请联系yuhongchun027@163.com(抚琴煮酒)

转载至 http://hi.baidu.com/yuhongchun027/blog/item/25eca12c3442e9e68a13998c.html

nginx 防盗链一则

2009年9月14日 没有评论
1
2
3
4
5
6
7
location ~* ^.+\.(gif|jpg|png|swf|flv|mp3|zip|rar)$ {
        valid_referers none blocked server_names  *.sitea.com *.siteb.com;
        if ($invalid_referer) {
            rewrite ^/   http://www.site.com/;
            #return 404;
        }
    }

可以自己注释选择是返回rewrite,或者404

分类: nginx相关 标签: ,

nginx gzip无法静态输出http

2009年3月18日 没有评论

最近在做nginx+squid2.6,发现静态文件nginx只要gzip后就http header就没有Content-Length,都是Transfer-Encoding chunked,使用gzip_static又必须每个文件预先压缩个gz。
而翻了翻squid手册说squid2.6输出gzip必须满足2点,1就是有Content-Length,2必须有vary信息。
网上搜了搜一时无法解决

分类: nginx相关 标签:

nginx配置文件中的rewrite参数中-f -d -e的使用误解

2009年3月8日 2 条评论

今天在写一个rewrite的时候总是不对,当中用到了文件不存在的判断,如下:

1
2
3
4
if (!-f $request_filename){
     rewrite ^(.*)$ http://site.com$1 permanent;
     break;
}

重温这几个参数的含义
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录

安装我原来的理解是如果需要判断请求的目录是否存在就用d,比如/dira/
需要判断请求的目录是否存在就用f,比如/dira/filea
如果文件或者目录之一有不存在的话就用e

原来我以为/dira/filea 假如dira和filea都不存在的话随便用那个参数都能匹配
今天经过实际使用发现我完全搞错了,如果真正请求遇到上面这个目录和文件都不存在的话 !-f 和 !-d 是没法匹配的,只能用 !-e 。
正确的解释是
-e和!-e用来判断是否存在文件目录

分类: nginx相关 标签: ,

nginx多域名站点rewrite跳转不改变域名的方法:server_name_in_redirec用法

2009年1月15日 没有评论

nginx中如果一个server节点有多个域名,并且在不是访问主域名的情况下如果进行rewrite跳转,会自动把域名转换成主域名。比如:

1
2
3
4
5
server {
   server_name www.my.com www.site.com;
   rewrite ^/$ /src/login.php redirect;
   ......
}

这里如果访问www.site.com的话会自动跳转到www.my.com/src/login.php,而不是www.site.com/src/login.php。

解决方法:
如果nginx版本在0.6.x及以上版本的话使用

1
2
3
4
5
6
server {
   server_name www.my.com www.site.com;
   server_name_in_redirect  off;
   rewrite ^/$ /src/login.php redirect;
   ......
}

老版本的话可以使用

1
2
3
4
5
6
7
server {
   server_name www.my.com www.site.com;
   location = / {
      rewrite ^ /src/login.php redirect;
   }
   ......
}

关键点:
如果server_name_in_redirec为on,那么nginx将使用server_name中的第一个 server name来进行rewrite跳转。如果设置成off的话nginx将使用请求中Request Headers中的host来进行rewrite跳转。
server_name_in_redirec 可以在配置文件中 http , server 和 location 区域级别中使用。

分类: nginx相关 标签: ,

nginx支持泛域名支持的方法

2009年1月15日 没有评论

要使用Nginx下的泛域名支持,必须在编译 Nginx的时候加上
–with-http_sub_module

然后使用

1
2
3
4
5
server {
listen       80;
server_name  _;
.....
}
分类: nginx相关 标签: ,

nginx配置文件中的rewrite参数

2009年1月15日 没有评论

正则表达式匹配,其中:
* ~ 为区分大小写匹配
* ~* 为不区分大小写匹配
* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配

文件及目录匹配,其中:
* -f和!-f用来判断是否存在文件
* -d和!-d用来判断是否存在目录
* -e和!-e用来判断是否存在文件或目录
* -x和!-x用来判断文件是否可执行

flag标记有:
* last 相当于Apache里的[L]标记,表示完成rewrite
* break 终止匹配, 不再匹配后面的规则
* redirect 返回302临时重定向
* permanent 返回301永久重定向

一些可用的全局变量有,可以用做条件判断(待补全)
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri

分类: nginx相关 标签: ,