Nginx简介
Nginx是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Nginx的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台
功能描述
继承Nginx-1.2.3 的所有特性,100%兼容Nginx的配置;
动态模块加载(DSO)支持。加入一个模块不再需要重新编译整个Nginx;
输入过滤器机制支持。通过使用这种机制Web应用防火墙的编写更为方便;
动态脚本语言Lua支持。扩展功能非常高效简单;
支持管道(pipe)和syslog(本地和远端)形式的日志以及日志抽样;
组合多个CSS、JavaScript文件的访问请求变成一个请求;
可以对后端的服务器进行主动健康检查,根据服务器状态自动上线下线;
自动根据CPU数目设置进程个数和绑定CPU亲缘性;
监控系统的负载和资源占用从而对系统进行保护;
显示对运维人员更友好的出错信息,便于定位出错机器;
更强大的防攻击(访问速度限制)模块;
更方便的命令行参数,如列出编译的模块列表、支持的指令等;
可以根据访问文件类型设置过期时间;
一.Nginx安装
# tar -zxvf nginx-1.5.0.tar.gz
# cd nginx-1.5.0
# ./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-openssl=/root/tools/openssl-1.0.1e –with-jemalloc=/usr/local/jemalloc-3.4.0
注意:–with-openssl=/root/tools/openssl-1.0.1e –with-jemalloc=/usr/local/jemalloc-3.4.0 这里指的是源码路径,不需要安装
遇到此错误 ./configure: error: the HTTP rewrite module requires the PCRE library.
原因:安装http_rewrite_module模块需要先安装PCRE开发包
解决方法 yum -y install pcre-devel
遇到此错误./configure: error: the HTTP SSL module requires OpenSSL library
原因:安装http_ssl_module模块需要openssl library
解决方法:yum install openssl-devel
二.Nginx 基本命令
/usr/local/nginx/sbin/nginx 启动
/usr/local/nginx/sbin/nginx -s reload 重启
/usr/local/nginx/sbin/nginx -s stop 关闭
设置nginx开机启动
vi /etc/init.d/nginx 编辑启动文件添加下面内容
#!/bin/sh
# chkconfig: – 55 45
# description: The nginx daemon is a web service.
# processname: nginx
. /etc/rc.d/init.d/functions
NGINX=”/usr/local/nginx/sbin/nginx”
start()
{
echo -n $”Starting nginx: ”
daemon $NGINX
echo
}
stop()
{
echo -n $”Shutting down nginx: ”
$NGINX -s stop
echo
}
quit()
{
echo -n $”Shutting down nginx: ”
$NGINX -s quit
echo
}
reload()
{
echo -n $”reload config:”
$NGINX -s reload
echo
}
[ -f $NGINX ] || exit 1
# See how we were called.
case “$1″ in
start)
start
;;
stop)
quit
;;
reload)
reload
;;
restart)
stop
sleep 3
start
;;
*)
echo $”Usage: $0 {start|stop|restart|reload}”
exit 1
esac
exit 0
保存退出
chmod 775 /etc/init.d/nginx #赋予文件执行权限
chkconfig –level 012345 nginx on #设置开机启动
chkconfig –level 3 nginx on
/etc/init.d/nginx restart
三.配置nginx
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
创建nginx用户www
groupadd www
useradd -g www www
编辑主配置文件
vi /usr/local/nginx/conf/nginx.conf
内容如下
user www;
worker_processes 1; #工作进程数,为CPU的核心数或者两倍
pid logs/nginx.pid;
events {
worker_connections 1024; #Linux最常用支持大并发的事件触发机制
}
http {
include mime.types; #设定mime类型,类型由mime.type文件定义
default_type application/octet-stream;
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
#access_log logs/access.log main; #设定请求缓冲
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
#server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include vhosts/*.conf; #nginx虚拟主机包含文件目录
}
编辑配置文件
cd /usr/local/nginx/conf/vhost/admin.conf
内容如下
server{
listen 80;
server_name 192.168.1.55;
root /home/admin/; #配置发布目录
access_log logs/www_lolfs.log main;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
root /home/admin/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.(cgi|pl)?$ {
gzip off;
root /usr/local/nagios/sbin;
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
fastcgi_pass unix:/usr/local/perl-fcgi/logs/perl-fcgi.sock;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
fastcgi_index index.cgi;
fastcgi_read_timeout 60;
#fastcgi_param REMOTE_USER $remote_user;
#auth_basic “Nagios Access”;
#auth_basic_user_file /usr/local/nagios/etc/nagiospasswd;
}
location ~* ^.+.(jpg|jpeg|gif|png|ico)$ {
access_log off;
expires 30d;
}
location ~* ^.+.(js|css)$ {
access_log off;
expires 1h;
}
location ~* ^.+.(html|php)$ {
access_log off;
expires 10m;
}
}
四.优化nginx系统的TCP设置
Vi /etc/syscrl.conf
net.ipv4.tcp_syncookies = 1
# 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1
# 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1
# 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
net.ipv4.tcp_fin_timeout
# 修改系統默认的 TIMEOUT 时间
sapi/fpm/init.d.php-fpm
net.ipv4.tcp_fin_timeout = 1
# 表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间
net.ipv4.tcp_keepalive_time = 1200
# 表示当keepalive起用的时候,tcp发送keepalive消息的频度,缺省是2小时,改为20分钟
net.ipv4.tcp_mem = 94500000 915000000 927000000
# 当tcp使用低于该值的内存页面数时,tcp不会考虑释放内存
net.ipv4.tcp_tw_reuse = 1
# 表示开启重用.允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭
net.ipv4.tcp_timestamps = 0
# 时间戳可以避免序列号的卷绕。1个1Gbps的链路肯定会遇到以前用过的序列号。时间戳能让内核接受这种“异常”的数据包,这里需要将其关闭
net.ipv4.tcp_synack_retries = 1
# 为了打开对端的连接,内核需要发送一个syn并附带一个回应前面一个syn的ack,也就是所谓三次握手中的第二次握手,这个设置决定了内核放弃连接之前发送syn+ack包的数量
net.ipv4.tcp_syn_retries = 1
# 在内核放弃建立连接之前发送syn包的数量
net.ipv4.tcp_tw_recycle = 1
# 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭
net.core.netdev_max_backlog = 262144
# 每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目
net.core.somaxconn = 262144
# web应用中listen函数的backlog默认会给我们内核参数net.core.somaxconn限制到128,而nginx定义的NGX_LISTEN_BACKLOG默认为511,所以有必要调整这个值
net.ipv4.tcp_max_orphans = 3276800
# 系统中最多有多少个tcp套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,连接将即刻被复位并打印出警告信息。这个限制仅仅是为了防止简单的dos攻击,不能过分依靠它或者人为地减少这个值,更应该增加这个值(如果增加了内存之后)
net.ipv4.tcp_max_syn_backlog = 262144
# 表示syn队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接网络连接数。对于有128M内存的系统而言,缺省值是1024,小内存的系统则是128
net.core.rmem_max = 16777216
# 最大socket读buffer,可参考的优化值:873200
net.core.wmem_max = 16777216
# 最大socket写buffer,可参考的优化值:873200
net.core.wmem_default = 8388608
# 表示发送套接字缓冲区大小的缺省值(以字节为单位)
net.core.rmem_default = 8388608
# 表示接收套接字缓冲区大小的缺省值(以字节为单位)使配置立即生效
/sbin/sysctl -P