centos7安装nginx,启动,配置,反向代理,日志
安装:使用yum安装
## 安装源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
## 开始安装
sudo yum -y install nginx
启停
# 开机启动
systemctl enable nginx.service
# 启动
systemctl start nginx
# 停止
systemctl stop nginx
# 重启
systemctl restart nginx
# 重新加载配置
nginx -s reload
配置
相关文件目录所在位置
配置文件目录: /etc/nginx/nginx.conf
安装目录: /etc/sysconfig/nginx
pidfile: /var/run/nginx.pidy
日志文件在: /var/log/nginx
日志分天保存配置
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 将原来的time_local修改为time_iso8601,该格式日期为“2017-01-19T09:10:52+08:00”
# 也可以其他格式,看个人习惯
log_format main '$remote_addr - $remote_user [$time_iso8601] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 按yyyy-mm-dd格式截取字符串,写入指定日志文件中
# 注意层次关系,这段脚本一定要加到server配置内部,且if要在access_log前面,否则set的变量将无法引用
server{
....
if ($time_iso8601 ~ '(\d{4}-\d{2}-\d{2})') {
set $tttt $1;
}
access_log logs/access-$tttt.log main;
....
}
执行 nginx -s reload 后则配置生效
nginx 反向代理配置
server {
listen 80;
server_name agent.example.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Server $host;
location / {
# 注意用能用就用内网IP
proxy_pass http://127.0.0.1:6666;
}
}
反向代理其他一些配置:
proxy_connect_timeout 1; #nginx服务器与被代理的服务器建立连接的超时时间,默认60秒
proxy_read_timeout 1; #nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。
proxy_send_timeout 1; #nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。
proxy_http_version 1.0 ; #Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本。
#proxy_method get; #支持客户端的请求方法。post/get;
proxy_ignore_client_abort on; #客户端断网时,nginx服务器是否终端对被代理服务器的请求。默认为off。
#Nginx服务器不处理设置的http相应投中的头域,这里空格隔开可以设置多个。
proxy_ignore_headers "Expires" "Set-Cookie";
#如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off。
proxy_intercept_errors on;
proxy_headers_hash_max_size 1024; #存放http报文头的哈希表容量上限,默认为512个字符。
proxy_headers_hash_bucket_size 128; #nginx服务器申请存放http报文头的哈希表容量大小。默认为64个字符。
# 反向代理upstream中设置的服务器组,出现故障时,被代理服务器返回的状态值。
# error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off
proxy_next_upstream timeout;
#默认为on,如果我们在错误日志中发现“SSL3_GET_FINSHED:digest check failed”的情况时,可以将该指令设置为off。
#proxy_ssl_session_reuse on;
多目录
location /download {
alias /data/static;
autoindex on; #开启索引功能
autoindex_exact_size off; # 关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; # 显示本机时间而非 GMT 时间
}
负载均衡配置
upstream mysvr {
# 权重请求顺序为:ABBABBABBABBABB....
server 192.168.10.121:3333 weight=1;
server 192.168.10.122:3333 weight=2;
ip_hash; #ip_hash:nginx会让相同的客户端ip请求相同的服务器。
}
server {
....
location ~*^.+$ {
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
}
}
ssl证书配置
server {
....
ssl on;
ssl_certificate /etc/nginx/cert/4242434_abc.cn.pem;
ssl_certificate_key /etc/nginx/cert/532q432543_abc.cn.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
}
gzip
server {
....
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript image/jpeg image/gif image/png;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
}
nginx websocket反向代理配置
Nginx代理webSocket经常中断的解决方法(也就是如何保持长连接)
现象描述:用nginx反代代理某个业务,发现平均1分钟左右,就会出现webSocket连接中断,然后查看了一下,是nginx出现的问题。
产生原因:nginx等待第一次通讯和第二次通讯的时间差,超过了它设定的最大等待时间,简单来说就是超时!
解决方法1
其实只要配置nginx.conf的对应localhost里面的这几个参数就好
proxy_connect_timeout;
proxy_read_timeout;
proxy_send_timeout;
解决方法2
发心跳包,原理就是在有效地再读时间内进行通讯,重新刷新再读时间
http {
....
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
....
server {
listen 8084;
server_name _;
root /usr/share/nginx/html/pcNet;
include /etc/nginx/default.d/*.conf;
location ^~/socket/ {
proxy_pass http://x.x.x.x:8080/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade websocket;
proxy_set_header Connection Upgrade;
}
....
}
}