Nginx的主配置文件详解
文章目录
- Nginx的主配置文件详解
- 如果是以rpm的方式安装,默认的配置文件目录为/etc/nginx/
- 如果是以2.2源码的方式安装,在没有指定–conf-path参数的情况,配置文件目录为/usr/local/nginx/conf
- 另外,可以通过include指令,加载某个目录下的.conf作为额外配置文件
[root@localhost ~]# cat /etc/nginx/nginx.conf
user nginx nginx; #指定运行nginx
worker_processes auto; #个数通常应该少于等于CPU物理核心数;
#daemon on|off; # 默认为on;确定nginx是否应该成为守护进程。主要用于开发过程中。调试时应该设置为off,其他模式都为on
#master_process on | off; # 默认为on;调试时设置为off
#worker_cpu_affinity CPUMASK; # CPU核心绑定worker proc,提升CPU缓存的命中率,不能避免context switch.
# CPUMASK: 0000 0000, 0000 0001, 0000 0010, 0000 0100, 0000 1000;
# timer_resolution INTERVAL; #减少工作进程中的计时器分辨率,从而减少gettimeofday()系统调用的次数。提高性能
#worker_priority number; # 默认为auto;定义工作进程的调度优先级,就像nice命令完成一样:否定 number 意味着更高的优先级。允许范围通常在-20到20之间变化。
#accept_mutex on | off; # 默认off;
error_log /var/log/nginx/error.log; # 错误日志;
pid /run/nginx.pid;
worker_rlimit_nofile 1024; # 指定所有worker进程能够打开的最大文件句柄数;
worker_rlimit_core 1024; # 更改RLIMIT_CORE工作进程的核心文件()的最大大小限制。用于在不重新启动主进程的情况下增加限制,默认不需要配置
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; # 加载额外配置文件
events {
worker_connections 1024; # 一个worker进程所能处理的最大并发连接数; 经常会需要调整;
#use epoll|select|poll|kqueue|/dev/poll|eventport; # 指定要使用的连接处理 method。通常不需要明确指定它,因为nginx默认使用最有效的方法。
#accept_mutex on | off; # 默认off; 如果accept_mutex启用,则工作进程将依次接受新连接。否则,将通知所有工作进程有关新连接的信息,如果新连接的数量很少,则某些工作进程可能只是浪费系统资源。
#accept_mutex_delay time; # 默认500ms;如果启用了accept_mutex,则指定当另一个工作进程当前正在接受新连接时,工作进程将尝试重新启动接受新连接的最长时间。
}
# 注意:与http相关的指令仅能够放置与http、server、location、upstream、if上下文,但有些指令仅应用于这5种指令的某些种;
http { # 由ngx_http_core_module模块所引入
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# 网络连接相关配置
keepalive_timeout 65; # 长连接的超时时长
keepalive_requests 100; # 在一个长连接上锁能够允许请求的最大资源数;
keepalive_disable msie6 # 为指定类型[msie6|safari|none]的User Agent禁用长连接;
tcp_nodelay on; # 默认为off,是否对长连接使用TCP_NODELAY选项;马上响应并提高用户体验应该on;
client_header_timeout 5; # 读取http请求报文首部的超时时长;
client_body_timeout 5; # 读取http请求报文body部分的超时时长;
send_timeout 5; # 发送响应报文的超时时长;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
upstream tomcatsrv {
#ip_hash;
server 10.241.96.101:80;
server 10.241.96.102:80;
}
server { # 每个server表示一个虚拟主机;类似httpd的VirtualHost;可以定义多个
listen 80 default_server;
listen [::]:80 default_server;
# 设置虚拟服务器的名称;可指定多个;支持通配符匹配;
# 通配符匹配优先级:
# 1. 先做精确匹配;
# 2. 左侧通配符匹配;*.chan.com
# 3. 右侧通配符匹配检查;如mail.*
# 4. 正则表达式匹配检查;如~^.*\.chan\.com$
# 5. default_server;
server_name web.chan.com;
# 默认主页面
index index.php index.html;
#设置资源路径映射;用于指明请求的RUL所对应的资源所在的文件系统上的起始路径;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
# 定义URL与本地文件系统的映射关系;类似httpd的Location
# location [ = | ~ | ~* | ^~ ] uri { ... }
# 功能:允许根据用户请求的URI来匹配定义的各location;匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能;
# 匹配方式:
# =: 精确匹配检查
# ^~: URI的前半部分匹配,不支持正则表达式;
# ~: 正则表达式模式匹配检查,区分字符大小写;
# ~*: 正则表达式模式匹配检查,不区分字符大小写;
# 优先级:
# 精确匹配(=)、^~、~、~*、不带任何符号的location;
location / {
proxy_pass http://tomcatsrv;
# 根据http响应状态码来指明特用的错误页dd面;
#error_page 404 /404.html;
# =200表示指定响应状态为200,并且指定自定义的错误页面;
#error_page 404 =200 /404_customed.html;
# 访问控制
deny 10.241.96.1; # 禁止某个IP或网络访问,值为all时禁止所有;
allow 10.241.96.0/24; # 允许该网段,值为all时允许所有
# 基于用户的访问控制basic,digest
auth_basic "Only for VIP."; # 认证名称
auth_basic_user_file /etc/nginx/user/.htpasswd; # 指定用户文件
}
# 开启nginx的状态监控stub_status
location /status {
stub_status on;
allow 10.241.96.1;
deny all;
}
# url重写; rewrite regex replacement flag;放置在location中例如:rewrite ^/images/(.*\.jpg)$ /imgs/$1 break; http://www.chan.com/images/a/b/c/1.jpg --> http://www.chan.com/imgs/a/b/c/1.jpg
# 后向引用使用$符号
# flag: last|break|redirect|permanent
# if语句
if ($http_user_agent ~* MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
# 防盗链
location ~* \.(jpg|gif|jpeg|png)$ {
valid_referers none blocked www.chan.com; # nginx自带的指令,合法的引用者为www.chan.com这个站点
if ($invalid_referer) {
rewrite ^/ http://www.chan.com/403.html;
}
}
#fastcgi的相关配置,构建LNMP,php要启用fpm模型;
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;
}
#location ~* \.(jsp|do)$ {
# proxy_pass http://tomcatsrv;
#}
# 根据http响应状态码来指明特用的错误页dd面;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}