nginx常用命令

本文介绍了Nginx的常用命令,如快速关闭、平稳关闭、重载配置等。还详细说明了在Linux下搭建Nginx环境的步骤,包括解压、配置、编译和安装等。此外,给出了Nginx.conf配置文件的示例,涵盖运行用户、进程、日志、工作模式等多方面配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • nginx -s stop :快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。

  • nginx -s quit :平稳关闭Nginx,保存相关信息,有安排的结束web服务。

  • nginx -s reload :因改变了Nginx相关配置,需要重新加载配置而重载。

  • nginx -s reopen :重新打开日志文件。

  • nginx -c filename :为 Nginx 指定一个配置文件,来代替缺省的。

  • nginx -t :不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。

  • nginx -v:显示 nginx 的版本。

  • nginx -V:显示 nginx 的版本,编译器版本和配置参数。

 

  • linux下搭建nginx环境
  • pwd 查看当前目录
  • cd /home/download 找到nginx安装包
  • tar -zxvf nginx-1.10.3.tar.gz 解压nginx安装包
  • cd nginx-1.10.3 进入nginx的目录
  • ./configure 运行nginx配置文件(如果出现错误,可能缺少库文件,安装后再执行这一步)
  • su 进入root权限,回车后输入密码
  • cd / 进入到根目录
  • yum -y install gcc gcc-c++ autoconf automake 安装gcc和gcc-c++(-y安装时选择同意选项,autoconf automake 自动配置自动安装,出现complete安装成功)
  • yum -y install pcre pcre-devel 安装pcre库
  • yum -y install zlib zlib-devel 安装zlip库
  • ./configure 进入到nginx目录再运行一次,直到成功后
  • make 编译
  • make install 安装nginx
  • cd /usr/local->ls 查看是否有nginx,如果有则安装完成
  • cd nginx conf目录放着配置文件 htmL放着网页程序 logs放着日志文件 sbin放着nginx的启动程序
  • /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 启动nginx
  • 浏览器打开localhost查看

在nginx目录下增加启动文件start.sh

rem 如果启动前已经启动nginx并记录下pid文件,会kill指定进程
nginx.exe -s stop

rem 测试配置文件语法正确性
nginx.exe -t -c conf/nginx.conf

rem 显示版本信息
nginx.exe -v

rem 按照指定配置去启动nginx
nginx.exe -c conf/nginx.conf

nginx.conf配置文件

#运行用户
#user somebody;

#启动进程,通常设置成和cpu的数量相等
worker_processes  1;

#全局错误日志
error_log  D:/Tools/nginx-1.10.1/logs/error.log;
error_log  D:/Tools/nginx-1.10.1/logs/notice.log  notice;
error_log  D:/Tools/nginx-1.10.1/logs/info.log  info;

#PID文件,记录当前启动的nginx的进程ID
pid        D:/Tools/nginx-1.10.1/logs/nginx.pid;

#工作模式及连接数上限
events {
   worker_connections 1024;    #单个后台worker process进程的最大并发链接数
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
   #设定mime类型(邮件支持类型),类型由mime.types文件定义
   include       D:/Tools/nginx-1.10.1/conf/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    D:/Tools/nginx-1.10.1/logs/access.log main;
   rewrite_log     on;
   
   #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
   #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
   sendfile        on;
   #tcp_nopush     on;

   #连接超时时间
   keepalive_timeout  120;
   tcp_nodelay        on;
   
   #gzip压缩开关
   #gzip  on;

   #设定实际的服务器列表 
   upstream zp_server1{
       server 127.0.0.1:8089;
   }

   #HTTP服务器
   server {
       #监听80端口,80端口是知名端口号,用于HTTP协议
       listen       80;
       
       #定义使用www.xx.com访问
       server_name  www.helloworld.com;
       
       #首页
       index index.html
       
       #指向webapp的目录
       root D: (自己服务器的目录) ;
       
       #编码格式
       charset utf-8;
       
       #代理配置参数
       proxy_connect_timeout 180;
       proxy_send_timeout 180;
       proxy_read_timeout 180;
       proxy_set_header Host $host;
       proxy_set_header X-Forwarder-For $remote_addr;

       #反向代理的路径(和upstream绑定),location 后面设置映射的路径
       location / {
           proxy_pass http://zp_server1;
       } 

       #静态文件,nginx自己处理
       location ~ ^/(images|javascript|js|css|flash|media|static)/ {
           root D:  (自己服务器的目录) ;
           #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
           expires 30d;
       }
   
       #设定查看Nginx状态的地址
       location /NginxStatus {
           stub_status           on;
           access_log            on;
           auth_basic            "NginxStatus";
           auth_basic_user_file  conf/htpasswd;
       }
   
       #禁止访问 .htxxx 文件
       location ~ /.ht {
           deny all;
       }
       
       #错误处理页面(可选择性配置)
       #error_page   404              /404.html;
       #error_page   500 502 503 504  /50x.html;
       #location = /50x.html {
       #    root   html;
       #}
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值