这个里面写了Nginx的常用命令,以及Nginx的日志配置,还有nginx.conf配置文件中,每一个参数的意思。

Nginx的常用命令

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

#启动nginx,直接运行nginx,就启动了,配置文件是默认的conf 目录下的nginx.conf文件 
./nginx 

#停止nginx
./nginx -s stop

#重启nginx
./nginx -s reload

#测试nginx
./nginx -t

Nginx配置文件与虚拟主机配置

nginx的配置文件在 conf目录下的nginx.conf文件,这个是nginx的配置文件,Nginx的代理是在第七层,应用层。

Nginx之简单使用及配置-yellowcong_其他

这个nginx.conf中,可以配置多个server ,而且每个Server 可以有自己的日志文件,这样我们可以根据访问的不同域名来分日志。

#设定线程数量,和自己的服务器CPU个数一样
#这个玩意配多了,也没用,配置少了,也发挥不了性能
worker_processes  1;

#配置连接的数量,默认是1024
events {
    worker_connections  1024;
}

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;

    #keepalive_timeout 0;
    #连接超时
    keepalive_timeout  65;

    #配置虚拟主机
    server {
     
       #server可以有多个
        #监听端口
        listen 81;

        #服务名称
        server_name localhost;

        #匹配路径,这个地方支持正则表达式
        location / {
      #这个路径是相对于nginx安装路径,
      #简单来说是nginx安装目录下的demo目录
          root  demo;
          index index.html;
        }

        #日志文件
        access_log  logs/demo.log  main;
    }
       #配置虚拟主机
    server {
     
       #server可以有多个
        #监听端口
        listen 80;

        #服务名称
        server_name localhost;

        #匹配路径,这个地方支持正则表达式
        location / {
      #这个路径是相对于nginx安装路径,
      #简单来说是nginx安装目录下的html目录
          root  html;
          index index.html;
        }

        #日志文件
        access_log  logs/access.log  main;
    }
}

这个是demo目录,是相对于nginx安装位置的,我们也可以在root配置绝对路径。
Nginx之简单使用及配置-yellowcong_nginx_02

日志

logs目录,里面存放在nginx的日志文件,里面有access.log(访问日志) ,error.log(错误日志)和nginx.pid(启动进程id)三个日志文件

Nginx之简单使用及配置-yellowcong_Nginx_03

这个nginx.pid用于存储启动的pid,当我们停止nginx的时候,就会查询这个pid文件
Nginx之简单使用及配置-yellowcong_Nginx_04

配置日志,nginx中日志的格式化,默认是man,我们也可以自定义日志的格式,这写日志的函数,都是nginx给我们提供的。
Nginx之简单使用及配置-yellowcong_Nginx_05

日志效果

tail -n -f 100 demo.log

Nginx之简单使用及配置-yellowcong_配置文件_06