Nginx 常用配置文件说明

Nginx 是一款开放源代码的高性能的HTTP服务器和反向代理服务器,同时支持IMAP/POP3代理服务。

Nginx模块分为内置模块和第三方模块,其中,内置模块中包括主模块与事件模块。

Nginx默认自动编译的模块,可以使用--without 参数禁用,可以使用--with 参数手动启动

Nginx可以通过--add-module = /path/module1的方法编译第三方模块

Nginx安装后分别为conf(主配置文件目录)、html(网站根目录)、logs(日志文件目录)、sbin(主程序目录)

Nginx 默认没有提供启动脚本,需要手动输入命令来管理进程

Nginx 会将进程号保存在/usr/local/nginx/logs/nginx.pid 


Nginx 配置文件解析

默认配置文件为/usr/local/nginx/conf/nginx.conf,配置文件主要包含全局、event、http、server设置,event主要用来定义nginx 工作模式,http 提供web 功能,server 用来设置虚拟主机,server必须位于http 内部,一个配置文件中可以有多个server

#设置用户组

#user nobody;


 # nginx对外提供web服务时的worker进程数。启动子进程数,可以通过ps aux|grep nginx  查看

worker_processes 1;

有关worker_processes官网给的相关信息: 最优值取决于许多因素,包括(但不限于)CPU核的数量、存储数据的硬盘数量及负载模式。不能确定的时候,将其设置为可用的CPU内核数将是一个好的开始(设置为“auto”将尝试自动检测它)

Defines the number of worker processes.

The optimal value depends on many factors including (but not limited to) the number of CPU cores, the number of hard disk drives that store data, and load pattern. When one is in doubt, setting it to the number of available CPU cores would be a good start (the value “auto” will try to autodetect it).

worker_rlimit_nofile 100000; 

worker_rlimit_nofile 更改worker进程的最大打开文件数限制。如果没设置,这个值为操作系统的限制。设置后操作系统和Nginx可以处理比“ulimit -a”更多的文件,把这个值设高,nginx就不会有“too many open files”问题了

(too many open files 相关:

出现这句提示的原因是程序打开的文件/socket连接数量超过系统设定值。

http://blog.csdn.net/jacson_bai/article/details/42171637

http://blog.csdn.net/l1028386804/article/details/51425325

)


#错误日志文件,以及日志级别

error_log logs/error.log info;


#进程号保存文件

pid logs/nginx.pid


events {

#每个进程可以处理的连接数,受系统文件句柄的限制

worker_connections 1024;

}


http{

#mime.type 为文件类型定义文件

include mime.type;

#默认文件类型

default_type  application/octet-stream;

#使用 log_format 可以自定义日志格式,名为main

#log_format main ' $remote_addr- $remote_user[$time_local] "$request" ‘

#      ' $status $body_bytes_sent "$http_referer" '

#      ' "$http_user_agent" "$http_x_forwarded_for"';


#创建访问日志,格式采用main定义的格式            

#access_log logs/access.log main;


#是否调用sendfile() 进行数据复制,sendfile() 复制数据是在内核级别完成的,所以会比一般的read、write 更高效

sendfile on;


#开启后服务器的响应头部信息产生独立的数据包发送,即一个响应头信息一个包

tcp_nopush on;


#保存连接的超时时间

keepalive_timeout 65;


#是否采用压缩功能,将页面压缩后传输更节省流量

gzip on;


#使用server 定义虚拟机

server {

#服务器监听的端口

listen     80;

#访问域名

server_name www.***.com;

#编码格式,如果网页编码与此设置不同,则将被自动转码

#charset utf8;

#设置虚拟主机的访问日志

#access_log logs/host.access.log main;

#对url进行匹配

location /{

#设置网页根路径,使用的是相对路径,html指的是处于nginx的安装路径下

root html

#首页文件,先找index.html,再找index.htm

index index.html index.htm

}

#设置错误代码对应的错误页面

#error_page 404      /404.html

#redirect server error pages to the static page/50x.html

error_page 500 502 503 504 /50x.html

location =/.50x.html{

root html;

}

#proxy the PHP scripts to Apache listening on 127.0.0.1:80

#下面三行注释表明,若用户访问URL 以.php 结尾,则自动将该请求,转交给127.0.0.1 服务器,通过proxy_pass 可以实现代理功能

#location ~ \.php ${

#proxy_pass   http://127.0.0.1;

#}

#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#location ~\.php${

#root html;

#fastcgi_pass 127.0.0.1:9000;

#fastcgi_index index.php;

#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

#include fastcgi_params;

#}

#deny access to .htaccess files,if Apache'documnet root concurs with nginx's one

#拒绝所有的人访问.ht 页面

#location ~ /\.ht{

#deny all;

#}

}

HTTPS server

#server{

#监听TLS 使用的443端口

#listen 443;

#server_name localhost;

#开启SSL功能

#ssl  on;

#指定证书文件,使用相对路径证书需要存放在与nginx.conf 同目录下

#ssl_cerificate cert.pem;

#指定私钥文件,使用相对路径私钥需要存放在与nginx.conf 同目录下

#ssl_certificate_key cert.key;

#ssl_session_timeout 5m;

……

location/{

root html;

index index.html index.htm

}

}                 

}

Nginx 多进程原理和特点:http://blog.csdn.net/qiangqiang_blog/article/details/45648835


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值