您的位置:首页 > 运维架构 > Nginx

Nginx之简单使用及配置-yellowcong

2017-11-07 21:16 316 查看
这个里面写了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.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配置绝对路径。



日志

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



这个nginx.pid用于存储启动的pid,当我们停止nginx的时候,就会查询这个pid文件



配置日志,nginx中日志的格式化,默认是man,我们也可以自定义日志的格式,这写日志的函数,都是nginx给我们提供的。



日志效果

tail -n -f 100 demo.log


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: