Nginx 安装(centos7)
首先安装必要的库(rewrite模块需要 pcre 库,ssl 功能需要openssl库)
安装目录:/usr/local
零..安装gcc gcc-c++(如新环境,未安装请先安装)
yum install -y gcc gcc-c++
一. 安装pcre
1. wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz
2. tar -zxvf pcre-8.36.tar.gz
3. /configure
4. make && make install
二. 安装ssl
1. wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
2. tar -zxvf openssl-1.0.1j.tar.gz
3. ./config
4. make && make install
三.安装nginx
1. wget http://nginx.org/download/nginx-1.8.0.tar.gz
2. tar -zxvf nginx-1.8.0.tar.gz
3. ./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module
(--with-http_ssl_module 以支持nginx.conf 中支持ssl:on)
4. make && make install
5. 修改profile /etc/profile末尾添加 export PATH=$PATH:/usr/local/nginx/sbin 并 source /etc/profile
6. nginx 启动
常用操作:
1.nginx -t 检查配置
2.nginx -s reload 重启
3.nginx.conf 中http下添加配置以记录nginx日志:
access_log /data/log/nginx/access.log;
error_log /data/log/nginx/error.log ;
Nginx目录
安装完后,配置目录下有以下文件:
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf
nginx.conf为主要配置文件,其余使用默认即可
Nginx.conf
nginx常规默认配置:
模块之外的配置:{
user : 用户(按默认配置即可)
pid : 默认设置即可
worker_process : (auto)表示工作进程的数量,一般设置为cpu的核数,设置auto将尝试自动检测
worker_rlimit_nofile : 更改worker进程的最大打开文件数限制。如果没设置的话,这个值为操作系统的限制。设置后你的操作系统和Nginx可以处理比“ulimit -a”更多的文件,当nginx报‘toomany open files’时,把此值调高。}
Events模块配置:{
worker_connections :(2048)设置可由一个worker进程同时打开的最大连接数。如果设置了上面提到的worker_rlimit_nofile,我们可以将这个值设得很高。最大客户数也由系统的可用socket连接数限制(~ 64K),所以设置不切实际的高没什么好处。
multi_accept :(on) 告诉nginx收到一个新连接通知后接受尽可能多的连接。
use :(epoll)设置用于复用客户端线程的轮询方法。Linux2.6+使用epoll,*BSD使用kqueue,在不设置的情况下,Nginx会选择一个最合适当前操作系统的。
}
HTTP模块{
server_tokens:(off) 并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。
sendfile:(on) 可以让sendfile()发挥作用。sendfile()可以在磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。Pre-sendfile是传送数据之前在用户空间申请数据缓冲区。之后用read()将数据从文件拷贝到这个缓冲区,write()将缓冲区数据写入网络。sendfile()是立即将数据从磁盘读到OS缓存。因为这种拷贝是在内核完成的,sendfile()要比组合read()和write()以及打开关闭丢弃缓冲更加有效(更多有关于sendfile)
tcp_nopush :(on) 告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送
tcp_nodelay :告诉nginx不要缓存数据,而是一段一段的发送–当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。
access_log 设置nginx是否将存储访问日志。关闭这个选项可以让读取磁盘IO操作更快(aka,YOLO)。
error_log 告诉nginx只能记录严重的错误。
keepalive_timeout 给客户端分配keep-alive链接超时时间。服务器将在这个超时时间过后关闭链接。我们将它设置低些可以让ngnix持续工作的时间更长。
client_header_timeout 和client_body_timeout 设置请求头和请求体(各自)的超时时间。我们也可以把这个设置低些。
reset_timeout_connection告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。
send_timeout 指定客户端的响应超时时间。这个设置不会用于整个转发器,而是在两次客户端读取操作之间。如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。
limit_conn为给定的key设置最大连接数。这里key是addr,我们设置的值是100,也就是说我们允许每一个IP地址最多同时打开有100个连接。
limit_conn_zone设置用于保存各种key(比如当前连接数)的共享内存的参数。5m就是5兆字节,这个值应该被设置的足够大以存储(32K*5)32byte状态或者(16K*5)64byte状态。
include只是一个在当前文件中包含另一个文件内容的指令。可用来包含其他nginx的conf。
default_type设置文件使用的默认的MIME-type。
charset设置我们的头文件中的默认的字符集。
性能提升的一些配置:
gzip是告诉nginx采用gzip压缩的形式发送数据。这将会减少我们发送的数据量。
gzip_disable为指定的客户端禁用gzip功能。我们设置成IE6或者更低版本以使我们的方案能够广泛兼容。
gzip_static告诉nginx在压缩资源之前,先查找是否有预先gzip处理过的资源。这要求你预先压缩你的文件(在这个例子中被注释掉了),从而允许你使用最高压缩比,这样nginx就不用再压缩这些文件了(想要更详尽的gzip_static的信息,请点击这里)。
gzip_proxied允许或者禁止压缩基于请求和响应的响应流。我们设置为any,意味着将会压缩所有的请求。
gzip_min_length设置对数据启用压缩的最少字节数。如果一个请求小于1000字节,我们最好不要压缩它,因为压缩这些小的数据会降低处理此请求的所有进程的速度。
gzip_comp_level设置数据的压缩等级。这个等级可以是1-9之间的任意数值,9是最慢但是压缩比最大的。我们设置为4,这是一个比较折中的设置。
gzip_type设置需要压缩的数据格式。
open_file_cache打开缓存的同时也指定了缓存最大数目,以及缓存的时间。我们可以设置一个相对高的最大时间,这样我们可以在它们不活动超过20秒后清除掉。
open_file_cache_valid 在open_file_cache中指定检测正确信息的间隔时间。
open_file_cache_min_uses 定义了open_file_cache中指令参数不活动时间期间里最小的文件数。
open_file_cache_errors指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。我们也包括了服务器模块,这些是在不同文件中定义的。如果你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。
}
完整配置样例:
user nginx;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 100000;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
access_log off;
error_log /var/log/nginx/error.log crit;
keepalive_timeout 10;
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;
limit_conn_zone $binary_remote_addr zone=addr:5m;
limit_conn addr 100;
include /etc/nginx/mime.types;
default_type text/html;
charset UTF-8;
gzip on;
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 6;
gzip_types 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_min_uses 2;
open_file_cache_errors on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
修改配置后需要重启nginx配置:
sudo service nginx restart
反向代理:
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,类型由mime.type文件定义
include /etc/nginx/mime.types;
default_type application/octet-stream;
#设定日志格式
access_log /var/log/nginx/access.log;
#省略上文有的一些配置节点
#。。。。。。。。。。
upstream pdf_service_frontends {
# 采用least_conn算法,把请求转发给连接数较少的后端
# 其他算法:ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
least_conn;
# server中IP后可附带的参数
#1.down 表示单前的server暂时不参与负载
#2.weight 默认为1.weight越大,负载的权重就越大。
#3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
#4.fail_timeout:max_fails次失败后,暂停的时间。
#5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
server 127.0.0.1:8323 max_fails=1 fail_timeout=20;
server 127.0.0.1:8324 max_fails=1 fail_timeout=20;
server 127.0.0.1:8325 max_fails=1 fail_timeout=20;
server 127.0.0.1:8326 max_fails=1 fail_timeout=20;
}
upstream pdf_service_2_frontends {
least_conn;
server 127.0.0.1:8331 max_fails=1 fail_timeout=20;
server 127.0.0.1:8332 max_fails=1 fail_timeout=20;
server 127.0.0.1:8333 max_fails=1 fail_timeout=20;
server 127.0.0.1:8334 max_fails=1 fail_timeout=20;
}
upstream itemrender_service_frontends {
server 127.0.0.1:8888 max_fails=1 fail_timeout=20;
}
server {
listen 8322;
server_name 127.0.0.1;
access_log /opt/var/log/nginx/host.pdf.access.log main;
location /anscard_item_render{
proxy_pass http://itemrender_service_frontends; #请求转向 定义的服务器列表
}
location /answer_card {
proxy_pass http://pdf_service_2_frontends;
}
location / {
proxy_pass http://pdf_service_frontends;
}
error_page 503 @maintenance;
location @maintenance{
rewrite ^(.*)$ /50x.html;
}
location = /50x.html {
root /usr/share/nginx/html;
}
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
}
}
期中负载均衡的配置:
1)、轮询(默认):每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2)、weight :指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
2)、ip_hash :每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
3)、fair(第三方):按后端服务器的响应时间来分配请求,响应时间短的优先分配。
4)、url_hash(第三方)
websocket配置:
proxy_set_header Host $host:$server_port;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400s; // 由于websocket为长时间链接,timeout时间需设置高些,或者采用定时心跳方案
其他资料:
http://my.oschina.net/duxuefeng/blog/34880
websocket相关:https://www.cnblogs.com/kevingrace/p/9512287.html
http://blog.youkuaiyun.com/zhangskd/article/details/50187407