安装环境:centos6.5 64位

nginx版本:nginx-1.4.7

一、安装

  1. 安装必要软件 pcre和openssl

    为了支持rewrite功能,我们需要安装pcre

tar zxvf pcre-8.31.tar.gz

cd pcre-8.31

./configure && make&& make install

    为了ssl支持,需要安装openssl

   yum -y install openssl*

2.安装nginx

解压:

tar -zxvf nginx-1.4.7.tar.gz

配置:

cd nginx-1.4.7

./configure --prefix=/home/server/nginx --with-pcre=/root/soft/pcre-8.31 --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module

编译和安装:

make && make install


--with-pcre=/root/soft/pcre-8.31 /////nginx安装路径

--with-http_stub_status_module   /////支持nginx状态查询

--with-http_ssl_module           ////支持https

--with-pcre=/root/soft/pcre-8.31 ///支持rewrite重写

--with-http_gzip_static_module  ////启用nginx ngx_http_gzip_static_module 支持(在线实时压缩输出数据流)

3.启动、关闭、重置nginx

   /home/server/nginx/sbin/nginx       ///启动

   /home/server/nginx/sbin/nginx  -s stop  ///关闭

   /home/server/nginx/sbin/nginx  -s reload //重置

   /home/server/nginx/sbin/nginx  -t  //检查配置文件

整个nginx的安装到这里就结束了。

二、内核参数优化

net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 1800
net.ipv4.ip_conntrack_max = 16777216 # 如果使用默认参数,容易出现网络丢包
net.ipv4.netfilter.ip_conntrack_max = 16777216# 如果使用默认参数,容易出现网络丢包
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries =
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.ip_local_port_range = 1024 65535

sysctl -p

三、配置文件优化

vi    nginx.conf

user  nobody nobody;   #运行nginx的所属组和所有者

worker_processes  8;     #nginx进程数(等同nginx总核心数)

error_log   /home1/logs/error.log crit; #错误日志路径

pid        logs/nginx.pid;   #pid路径


events {
    worker_connections  65535; #一个进程可以同时处理65535个链接请求
    multi_accept on;   #告诉nginx收到一个新连接通知后接受尽可能多的连接

   use epoll; #设置客户端线程轮询方法,linux内核2.6+应使用epoll,高性能网络模型
}


upstream  server.com  {
       server 10.10.10.111:80 max_fails=2 fail_timeout=15s;   #代理服务器

     }

http   {


include      mime.types;  #只是一个在当前文件中包含另一个文件内容的指令

default_type text/html;  #默认文件类型
charset UTF-8;  #文件的默认字符集


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  #默认访问日志路劲


sendfile  on; #开启高效传输模式

server_tokens off;    #隐藏版本号

tcp_nopush on; #告诉 nginx 在一个数据包里发送所有头文件,而不一个接一个的发送

tcp_nodelay on; #告诉 nginx 不要缓存数据,而是一段一段的发送–当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。

keepalive_timeout 65;  #keepalive 超时时间

reset_timedout_connection on; #告诉 nginx 关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间

open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;   #日志文件缓存,off:禁用缓存;

参数注释如下:

max: 设置缓存中的最大文件描述符数量

inactive:设置存活时间,默认是 10s
min_uses:设置在 inactive 时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是 1 次
valid:设置检查频率,默认 60s


server_names_hash_bucket_size 128;  #根据server名字的记录其hash表的大小。

client_header_buffer_size 512k;   #上传文件大小限制。

large_client_header_buffers 32 128k;  #大请求缓冲区的个数和大小。

client_max_body_size 100m;  #允许客户端连接的最大请求实体大小为100m。


gzip on;  #告诉 nginx 采用 gzip 压缩的形式发送数据。这将会减少我们发送的数据量

#开始配置一个域名,一个server配置段一般对应一个域名

gzip_disable "msie6";  #为指定的客户端禁用gzip功能。设置成IE6使之兼容。

gzip_static on; #告诉nginx在压缩资源前,先查找是否有预先gzip处理过的资源。

gzip_proxied any; #允许或者禁止压缩基于请求和响应的响应流,any将会压缩所有的请求。

gzip_min_length 1k; #最小压缩文件大小。

gzip_comp_level 4;  #设置数据压缩等级,9最慢压缩比最大,取折中值。

gzip_vary on;  #和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持。为避免浪费不支持的也压缩,需要根据客户端的HTTP头来判断,是否需要压缩。
gzip_http_version 1.0;  #识别http的协议版本

gzip_buffers 4 16k;  #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存。

gzip_types text/htm text/shtml text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;   #设置需要压缩的数据格式。


open_file_cache max=100000 inactive=20s;  #打开缓存的同时也指定了缓存的最大数目,以及缓存的时间,

open_file_cache_valid 30s;  #在 open_file_cache 中指定检测正确信息的间隔时间。

open_file_cache_min_users 2;  #定义了open_file_cache中指令参数不活动时间期间里最小的文件数。

open_file_cache_errors  on;  #指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。


ssi on;  #开启ssi支持

ssi_silent_errors on;  #开启后处理ssi文件出错时,不输出错误提示。

ssi_types text/shtml text/xml;  #默认是ssi_types text/html,所以如果需要htm和html支持,则不需要设置这句,如果需要shtml支持,则需要设置:ssi_types text/shtml

server {

listen  80;  #在本机所有 ip 上监听 80,也可以写为 192.168.35.201:80,只监听 该IP上的 80 口

server_name    www.test.com;     #域名 

root  /home/web;   #站点根目录

index index.html index.htm; #索引文件

error_page   404 /404x.html;    #定义错误页面


location = /index.shtml {
                expires -1;     #页面立即过期
        }


location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
        expires 30d;      #图片缓存时间
}


location ~* ^/wap/.*$ {
            proxy_pass http:/server.com;
            proxy_redirect default;
        }

}

}

简单的写这么多,还有很多没写,以后我熟悉了再写吧!