Skip to main content

米斯特周

Tag: Nginx

weblogic前端代理应用于nginx

## 前言

客户给我们了几台vps,主要是为了给我们的应用使用,目前我们的应用程序运行在A机和B机两台VPS上,使用了weblogic的集群,但是,有一个proxy应用安装在A机上,专门负责分发应用,这样的话,无形中增加了A机的负载,年初就因为这个事情,A机每天高峰期就宕机,我又拿了一台闲置的vps专门跑应用的计划任务,目前一共A,B,C三台vps在跑应用,最近开始关注这个事情,为什么不能使用nginx去做负载呢?我还有一台闲置的vps啊,我可以安装nginx专门做负载,转发流量给其他三台,这样A机就不会有负载问题了。

在CentOS 6.2上安装Nginx+PHP5(PHP-FPM)+MySQL

## 1.使用非官方软件源

[root@localhost /]# rpm --import https://fedoraproject.org/static/0608B895.txt
[root@localhost /]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm
[root@localhost /]# rpm –import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
[root@localhost /]# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
[root@localhost /]# yum install yum-priorities

编辑/etc/yum.repos.d/epel.repo文件,启用这个源,使其优先级最高

[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1 p
riority=10
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
[...]

some nginx rewrite rules

客户要用DISCUZ作为交流论坛,从网上找了一个DISCUZ规则,发现不对,现发出正确的规则。

rewrite ^([^.]_)/topic-(.+).html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^.]_)/article-([0-9]+)-([0-9]+).html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
rewrite ^([^.]_)/forum-(\w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^.]_)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^.]_)/group-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^.]_)/space-(username|uid)-(.+).html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^.]_)/blog-([0-9]+)-([0-9]+).html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
rewrite ^([^.]_)/(fid|tid)-([0-9]+).html$ $1/index.php?action=$2&value=$3 last; if (!-e $request_filename) { return 404; }

nginx预防SQL注入的方法

nginx作为一款轻量级的web服务器,以其高并发,高性能被多数IT公司采用作为替换apache服务器的首选程序。

前几天客户这边要求在nginx端做SQL注入防护的措施,可按nginx的功能来说,nginx不具备应用防火墙的功能。现在只能求助于伟大的google了,好在发现了两种办法。第一种办法就是过滤url基本的注入关键字,达到初步的预防措施。第二种办法,使用数据库防火墙,目前的可选软件可以考虑GreenSQL,相当的便捷,它提供Windows,Debian,RH/CentOS安装包,包括32位和64位两种版本,其作为数据库防火墙,接受web端发起的所有数据库连接请求,过滤SQL,假如属于注入等其他非法请求,GreenSQL会返回一个空数据集,不会影响到前端WEB应用,我在一台测试服务器上安装使用了GreenSQL Express,从速度先说,还是很不错的,以Wordpress为例,打开页面没有感觉到慢,感觉不到数据库防火墙的存在。

第一种方法:(来自google)

error_page 519 /519.html;
if ($request_uri ~* "(cost\()|(concat\()") {
	return 519;
}
if ($request_uri ~* "[+|(%20)]union[+|(%20)]") {
	return 519;
}
if ($request_uri ~* "[+|(%20)]and[+|(%20)]") {
	return 519;
}
if ($request_uri ~* "[+|(%20)]select[+|(%20)]") {
	return 519;
}

CentOS下nginx+php+fastcgi笔记

Centos版本5.7,首先需要做的进行内核优化。下面是内核优化参数

# 内核优化

net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0 kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000

Ubuntu 9.04 Nginx+php+mysql

由于Ubuntu 9.04已经包含了nginx,所以根本不要编译,安装超简单!

修改/etc/apt/sources.list文件内容为国内镜像,然后运行:

apt-get update apt-get install nginx

即可完成安装。启动nginx:

/etc/init.d/nginx start

然后就可以访问http://localhost/了,一切正常!如果不能访问,先不要继续,看看是什么原因,解决之后再继续。