Nginx合集-常用功能指导


1)启动、重启以及停止nginx

进入sbin目录之后,输入以下命令

#启动nginx
./nginx

#指定配置文件启动nginx
./nginx -c /usr/local/nginx/conf/nginx.conf

#重启nginx
nginx -s reload

#停止nginx
nginx -s stop

#nginx版本查询
nginx -V

sbin目录检查,若不知道sbin目录地址,可以通过以下命令操作

ps -ef|grep nginx

编辑

通过systemctl的启动方式

#查看nginx状态
systemctl status nginx

#启动nginx
systemctl start nginx

#重启nginx
systemctl restart nginx

#停止nginx
systemctl stop nginx

2)检测配置文件是否有语法错误(若不知道配置文件路径,也可以通过该命令获取到)

一般在编辑nginx配置文件之后,需要执行该命令判断是否存在语法错误,无问题,才可重启nginx使配置文件生效。

nginx -t

如果配置文件无问题,提示ok(
/usr/local/nginx/conf/nginx.conf配置文件路径)。

编辑

如果存在语法错误,会提示报错原因和行数。可以通过vim编辑器打开文件并设置行数查看修改。

编辑

编辑

3)查看nginx占用端口号

nginx启动成功之后,可以输入以下命令查看占用端口。

注意:针对nginx占用端口需要合理合规,若存在不用的端口,请在配置文件中删除并重启nginx

备注:谷歌禁用端口 10080,一般不使用

netstat -anp | grep nginx

编辑

4)配置文件说明

#root用户访问
user  root;
#CPU核数,一般同服务器的cpu,或者设置auto
worker_processes 16;  

#nginx错误日志目录
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#nginx进行id记录文件目录
pid        logs/nginx.pid;
#load_module /usr/lib/nginx/modules/ngx_stream_module.so;


events
{
    worker_connections 65535; #支持客户端最大链接数
}


http
{
    include mime.types;
    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;
	  #tcp_nodelay on;

    #隐藏版本目录
    server_tokens       off;

    #客户端请求头开启下划线识别
	  underscores_in_headers on;


    #超时时间相关配置
    keepalive_timeout 65;  #第一个参数指定客户端连接保持活动的超时时间,第二个参数是可选的,它指定了消息头保持活动的有效时间
    proxy_connect_timeout 60s;  #后端服务器连接的超时时间_发起握手等候响应超时时间
    proxy_send_timeout 60s;  #后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
    proxy_read_timeout 60s;  #连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)

	  client_body_timeout 10;  #设置客户端请求主体读取超时时间
	  client_header_timeout 30;  #设置客户端请求头读取超时时间
	
	  #可解决504 Gateway Time-out问题
	  send_timeout 10;  #指定响应客户端的超时时间
	  fastcgi_buffers 32 32k;  #设置fastcgi缓冲区为32+32×32k=1056,getconf PAGESIZE查看系统本身分页大小(单位bytes)
	
	  client_max_body_size 20M;      #设置客户端上传最大文件大小,如果继续报错继续加大
    client_body_buffer_size 256K;  #设置缓冲区大小提高nginx效率

    
	  include conf.d/*.conf;
}

编辑

	
#网关负载均衡
upstream gatway.com
    {
	    #weight权重值(越大访问率大),在fail_timeout时间内检查后端服务器max_fails次,失败则被剔除;
        server gatway:8083 weight=5 fail_timeout=30s max_fails=5;
        keepalive 256;
    }	



#upstream  miniprogramServer {
#        server 191.11.1.10:6070 weight=1;
#        server 191.11.1.12:6070 weight=2; 
#}


#入口
server {
        listen 80 ssl;
        server_name xxx.xxx.xxx.xxx app.cn ;
		    #安全:隐藏版本目录
        server_tokens  off;
        ssl_certificate ca/_.app.cn.crt;
        ssl_certificate_key ca/_.app.cn.key;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
		    access_log  logs/apitest.cocodalaapp.cn.access.log  main;        
        

        location ^~ /
        {
            proxy_pass http://app.com/;
        }


        location /xxx
        {
            add_header 'Access-Control-Allow-Origin' *;
            alias /opt/xxx-front/;
            index index.html index.htm;

       }        
         
        location ^~ /xxx-xxx
        {
            if ($request_method = 'OPTIONS')
            {
								#解决跨域配置
                add_header 'Access-Control-Allow-Origin' '*' always; 
                add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE' always;  
                add_header 'Access-Control-Allow-Headers' '*' always;
                add_header 'Access-Control-Max-Age' 1728000 always;
                add_header 'Content-Length' 0;
                add_header 'Content-Type' 'text/plain; charset=utf-8';
                return 204;
            }

          	#安全:限制HTTP请求方法
            if ($request_method ~* '(GET|POST|DELETE|PUT)')
            {
                add_header 'Access-Control-Allow-Origin' '*' always;
            }
            proxy_pass http://xxx.xxx.xxx.xxx:9111/;
        }


		    #安全:错误页面重定向
        error_page 500 502 503 504 /50x.html;

        location = /50x.html
        {
            root html;
        }

    }
        

5)防火墙策略配置

登录 · 数字化产品部

6)nginx日志切分

# 创建nginx日志切割脚本
vi /usr/local/src/nginxlog.sh

#!/bin/bash

logs_path="/usr/local/nginx/logs/"  #nginx部署完成该目录存在
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y-%m-%d").log

kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`   #nginx部署完成该目录存在

find ${logs_path}$(date -d "yesterday" +"%Y") -type f -mtime +7 -exec rm -rf {} \;
find ${logs_path} -maxdepth 1 -type d -empty  -exec rmdir {} \;
# 赋权文件
chmod +x /usr/local/src/nginxlog.sh


# crontab 脚本写入crontab 每天0点执行
0 0 * * * /bin/bash /usr/local/src/nginxlog.sh

相关文章

Nginx 从安装到高可用入门教程

Nginx安装1、去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本2、上传nginx到linux系统3、安装依赖环境(1)安装gcc环境yum install gcc-...

Nginx从安装到高可用,一篇搞定

一、Nginx安装1、去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本2、上传nginx到linux系统3、安装依赖环境(1)安装gcc环境yum install gc...

Nginx的启动、停止与重启方法

启动 启动代码格式:nginx安装目录地址 -c nginx配置文件地址例如:[root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /usr/...

nginx启动、重启、关闭

一、启动cd usr/local/nginx/sbin./nginx二、重启更改配置重启nginxkill -HUP 主进程号或进程号文件路径或者使用cd /usr/local/nginx/sbin....

老版本nginx存在安全漏洞,请升级至1.18稳定版本

1、场景介绍部门承接了XXX局的安全防控系统,物理机部署在本地XX云上,web服务器使用nginx服务,采用动静分离的部署方式,nginx版本1.14,应XX云安全检测要求现版本暴出漏洞,需要进行版本...