nginx基础(二)- 基本配置

目录

1、参数配置

1. 用户名配置

备份并修改nginx.conf,修改第一行,将

#user nobody;
# 修改为
user nginx;

2. http安全漏洞修复

针对安全部提出的以下漏洞:Web服务器配置为允许使用危险的HTTP方法,如PUT、MOVE、COPY、DELETE、PROPFIND、SEARCH、MKCOL、LOCK、UNLOCK、PROPPATCH,该配置可能允许未授权的用户对Web服务器进行敏感操作。

修复方案如下;

在nginx.conf中的server部分添加:

if ($request_method !~ ^(GET|POST|HEAD)$) {
        return 403;
}

3. 日志配置

#error_log  logs/error.log  info;
# 修改为
error_log  /opt/nginx/logs/error.log  info;

有点多余了,尽量保持在nginx主目录下,方便查看,生产环境下,需要特别大的日志量时会使用特殊的目录进行以及目录大小来保存日志文件,所以需要

4. 连接数配置

备份并调整nginx.conf的

1、将worker_proccesses配置为CPU核心数

worker_processes 8; 

2、将worker_connections 设置为10000,也可根据要求调整,注意ulimit -n的值必须大于worker_connections

worker_connections 10000

5. 性能参数配置

添加到http中

第一个参数需要添加到顶头

#服务器名字的hash表大小(第一个参数需要添加到顶头)
server_names_hash_bucket_size 128;
#设定请求缓存
client_header_buffer_size 32k;
large_client_header_buffers 4 64k;
#上传文件大小设置
client_max_body_size 50m;
#开启高效文件传输模式
sendfile on;
#防止网络阻塞
tcp_nodelay on;
#长连接超时时间,单位是秒
keepalive_timeout 80;

#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;

#gzip模块设置
#开启gzip压缩输出
gzip on;
#最小压缩文件大小
gzip_min_length  1k;
#压缩缓冲区
gzip_buffers     4 16k;
#压缩版本
gzip_http_version 1.0;
#压缩等级
gzip_comp_level 2;
#压缩类型
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;

6. 反向代理及负载均衡配置

假设要把nginx:80端口文根/test/ 转到后端两台服务器10.186.28.223、10.186.28.198处理

1)创建upstream

    upstream proxy_svrs
    {
        sticky;
        server 10.186.28.223:8080;
        server 10.186.28.198:8080;
	}

​ 说明:sticky为sticky模块提供的基于cookie的会话保持。

​ 此外,sticky可以配置expires指定cookie过期时间,比如expires=30m,expires=1h,默认浏览器关闭就过期。

​ 补充:sticky不能和ip_hash同时使用,如果什么都不配置nginx将会轮询发送后端请求。

2)配置指定文根反向代理

location /test/ {
            proxy_pass http://proxy_svrs;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            proxy_buffer_size 64k;
            proxy_buffers   8 32k;
            proxy_busy_buffers_size 64k;
            proxy_connect_timeout 90;
            proxy_read_timeout 90;
            proxy_send_timeout 90;
        }

说明:默认参数见http://nginx.org/en/docs/http/ngx_http_proxy_module.html#directives,几个timeout的默认都是60s。

7. 静态资源缓存配置(可选)

#图片缓存时间设置
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    expires 10d;
    }
    #JS和CSS缓存时间设置
    location ~ .*\.(js|css)?$
    {
    expires 1h;
    }

8. TCP代理配置(可选,需启用stream模块)

stream {
    upstream tcpserver {
        hash $remote_addr consistent;
        server 10.186.28.223:9091 max_fails=10 fail_timeout=60s;
    }

    server {
        listen 12345;
        proxy_pass tcpserver;
        proxy_timeout 300s;
    }
}

10.186.28.223:9091指的是后端IP:端口,可以添加多个server

listen 12345; 指的是nginx用于做tcp socket代理的监听端口

max_fails=10 fail_timeout=60s

这俩是关联的,如果某台服务器在fail_timeout时间内出现了max_fails次连接失败,那么nginx就会认为那个服务器已经挂掉,从而在 fail_timeout时间内不再去查询它,fail_timeout的默认值是10s,max_fails的默认值是1(这意味着一发生错误就认为服务器挂掉),如果把max_fails设为0则表示把这个检查取消。

9. 使用include方法,优化nginx.conf文件(可选)

在http全局块中插入include 目录/*.conf\

如:

http{
	include vhosts/*.conf
}

把所有的反向/正向代理的server合理的分开管理

2、自启动及日志轮转配置

1. 添加开机自启动脚本

cd /opt/nginx/

创建启动脚本文件

vi NginxAutoStart.sh
#!/bin/sh
APPLOG=/opt/nginx/logs
echo "`date +%F` `date +%R`  AutoStarting......" >> $APPLOG/autorun.log

sudo /opt/nginx/bin/nginx

echo "Nginx Start Complete!" >> $APPLOG/autorun.log
echo "AutoStart Complete!" >> $APPLOG/autorun.log

设置可执行权限

chown -R nginx:nginx /opt/nginx/
chmod +x /opt/nginx/NginxAutoStart.sh

rc.local中配置启动脚本

vim /etc/rc.d/rc.local
# 添加如下内容:nginx自启动(开机启动)
#su - nginx -c "/opt/nginx/NginxAutoStart.sh" # su - 用户名需要该用户具有登陆权限
/opt/nginx/NginxAutoStart.sh

设置rc.local可执行权限

chmod +x /etc/rc.d/rc.local

2. 添加日志切割定时清理日志脚本

cd /opt/nginx

编辑脚本:

vi AutoClear.sh
#!/bin/sh
date=`date +%Y%m%d%H%M`

cd /opt/nginx/logs

cp error.log error_$date.log
> error.log

cp access.log access_$date.log
> access.log

find /opt/nginx/logs -type f -name "error_*.log" -mtime +15 -exec rm -f {} \;
find /opt/nginx/logs -type f -name "access_*.log" -mtime +15 -exec rm -f {} \;

设置可执行权限

chown -R nginx:nginx /opt/nginx/
chmod +x /opt/nginx/AutoClear.sh

设置定时任务

crontab -e
# 添加如下定时任务
* 12 * * * /bin/sh /opt/nginx/AutoClear.sh > /dev/null

每天12点执行一次