NGINX的配置文件
1.1 nginx.conf配置文件简单介绍
# 全局配置端,对全局生效,主要设置nginx的启动用户/组,启动的工作进程数量,工作模式,Nginx的PID路径,日志路径等。
# 在这里是过滤掉了以# 开头的行和空白行
[root@localhost@~]->grep -v "#" /apps/nginx/conf/nginx.conf |grep -v "^$"
user nginx;
worker_processes 1;
# 启动工作进程的数量
pid logs/nginx.pid;
events {# event设置块,主要影响nginx服务器与用户的网络连接,比如是否允许同时接受多个网络连接,用哪个事件驱动模型处理请求,每个工作进程可以支持的最大连接数,是否开启多工作进程下的网络连接进行序列化等;
worker_connections 1024;
# worker_connections 设置单个nginx工作进程可以接受的最大并发,作为web服务器的时候最大并发数为worker_connections * woker_processes 作为反向代理的时候为该值的1/2
}
http {# http块是nginx服务器配置中的重要部分,缓存、代理和日志格式定义等绝大多事功能和第三方模块都可以在这里设置,http块可以包含多个server块,而一个server块中又可以包含多个location块,server块可以配置文件引入,MIME-TYPE定义,日志自定义,是否启用sendfile、连接超时时间和单个连接的请求上线等。
include mime.types;
default_type application/octet-stream;
sendfile on;
# 作为web服务器的时候打开sendfile加快文件传输
keepalive_timeout 65;
# 连接超时时长,单位为秒
server {# 设置一个虚拟主机,可以包含自己的全局块,同时也可以包含多个location块,比如虚拟机监听的端口,本虚拟机的名称和IP配置,多个server可以使用一个端口,比如都使用80端口提供web服务
listen 80;
# 配置server监听端口
server_name localhost;
# 本server名称,当访问此名称的时候Nginx会调用当前server内部的配置进程匹配
location / {# location其实是server的一个指令。为nginx服务器提供指令,根据nginx接受到的请求字符串,对用户的URI进行匹配,来处理特定的功能,很多第三方模块配置也是在此模块中配置的
root html;
# 相当于默认界面的目录名称,默认是相对路径,可以用绝对路径
index index.html index.htm;
# 默认界面文件名
}
error_page 500 502 503 504 /50x.html; # 错误页面的文件名
location = /50x.html {
root html;
}
}
#和邮件相关的配置
#mail {
# ...
# } mail 协议相关配置段
#tcp代理配置,1.9版本以上支持
#stream {stream 服务器相关配置段
# ...
# }
#导入其他路径的配置文件
#include /apps/nginx/conf.d/*.conf
}
1.2 Nginx配置文件详细介绍
[root@localhost@~]->cat /apps/nginx/conf/nginx.conf
#####################以下是全局配置#########################
user nginx;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
#####################以下是http配置#########################
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;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
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
#
#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's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
1.2.1 全局配置
user nginx;
# 启动Nginx的用户和组
worker_processes [num | auto];
# 启动时的工作进程数
worker_cpu_affinity 00000001 00000010 00000100 00001000;
# 将工作进程绑定在指定的cpu核心,当有4核以上就这样配,减少工作进程在不同cpu核心的跳转,提升性能
# 通过以下指令查看工作进程所在核心的位置
[root@localhost@~]->ps axo pid,cmd,psr |grep nginx
1568 nginx: master process /apps 1
1569 nginx: worker process 0
1673 grep --color=auto nginx 0
# 错误日志的配置:error_log file [debug | info | notice | warn | error | crit | alert |emerg]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# pid 文件保存路径
pid logs/nginx.pid;
worker_priority 0;
# 工作进程优先级,-20~19
work_rlimit_nofile 65536;
# 这个数字是nginx的最大连接数,该数字不能超过系统级别的最大打开文件数的限制
daemon off;
# 前台运行,一般调试时候用
master_process off|on;
# 开启关闭单主进程工作模式
events {
worker_connections 65536;
use epoll;
# 使用epoll事件驱动,还可以选择select、poll,该设置只能设置在events块中
accept_mutex on;
# 当只有一个请求时,只有一个睡眠进程被唤醒;默认为off,全部唤醒的过程叫做"惊群"
multi_accept on;
# 设置每个工作进程可以同时接受多个新的网络连接,打开后可以同时接受多个
}
1.2.2 http配置
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;
# 指定是否使用sendfile系统调用来传输文件,被称为零拷贝,从硬盘->kernel buffer->协议栈
#tcp_nopush on;
# 开启了sendfile后,合并请求后统一发送给客户端
#tcp_nodelay off;
# 开启keepalived模式下的连接是可以配置,off 合并多个请求后再发送,on 立即发送相应数据
keepalive_timeout 65 65;
# 设置会话保持时间,一般有两个值,前面的是实际秒,后面的是发送给客户端的超时时长
#gzip on;
# 开启文件压缩
server {
listen 80;
# 监听端口
server_name localhost;
# 可以以空格隔开写多个server_name并支持正则表达式
#charset koi8-r;
# 编码格式,默认是俄语格式,可以改成UTF-8;
#access_log logs/host.access.log main;
location / {
root html;
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
#
#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$ {# 以fastcgi的方式转发php请求给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's document root
# concurs with nginx's one
#
#location ~ /\.ht {# 拒绝web访问指定文件,如以.htaccess结尾的文件
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {# 自定义虚拟server
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# ngx_http_index_moudle模块提供功能
# }
#}
# HTTPS server
#
#server { # https相关
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
1.3 开始尝试基于不同IP端口配置虚拟主机
该功能依赖ngx_http_core_moudle
实现
1.3.1 新建一个PC站点
[root@localhost@~]->cd /apps/nginx/conf/
[root@localhost@conf]->mkdir conf.d
# 在conf.d文件夹下新建pc.conf
[root@localhost@conf.d]->cat pc.conf
server {
listen 80;
server_name www.flamenca.net;
location / {
root /data/nginx/html/flamenca;
}
}
# 指定了网页文件位置/data/nginx/html/flamenca/
# 建立该文件路径
[root@localhost@conf.d]->mkdir /data/nginx/html/flamenca -pv
# 将pc.conf文件加入nginx.conf配置中
# 在nginx.conf的http块中加入这一句
include /apps/nginx/conf/conf.d/*.conf;
# 重载nginx
# 在win10的hosts文件中添加
192.168.33.3 www.flamenca.net
1.2.2 相同的再建立一个mobile web站点
1.3 location块中的root和alias区别
root
root 指定web站点的家目录,文件的绝对路径等于 root+location
比如在上面配置中的location
location / {
root /data/nginx/html/flamenca;
}
实际路径为:/data/nginx/html/flamenca/
默认打开的是 /data/nginx/html/flamenca/index.html
##################################################
以此类推
location /abc {
root /data/nginx/html/flamenca;
}
实际路径为 /data/nginx/html/flamenca/abc
默认打开的是/data/nginx/html/flamenca/abc/index.html
alias
alias 自定义路径别名,可以重定向路径
server {
listen 80;
server_name www.flamenca.net;
location / {
root /data/nginx/html/flamenca;
}
location /abc {
alias /data/nginx/html/flamenca;
index index.html;
}
}
# /abc路径要事先建立
# 当访问www.flamenca.net/abc时会访问到/data/nginx/html/flamenca/index.html
[root@localhost@conf.d]->mkdir /data/nginx/html/flamenca/abc
[root@localhost@conf.d]->echo "abc page" > /data/nginx/html/flamenca/abc/index.html
1.4 location配置
在没有使用正则表达式前,nginx会在location设置中选取一个匹配度最高的URI,如果匹配成功就结束搜索,并使用该location内的设置处理请求。
location中的正则表达式:
语法:
location [=|~|~*|^~] /uri/ {...}
= 精准匹配
~ 区分大小写
~* 不区分大小写
!~ 区分大小写不匹配
!~* 不区分大小写不匹配
^ 匹配开头
$ 匹配结尾
\ 转义符
* 任意长度任意字符
匹配优先级:=, ^~,~/~*,/
= > 完整路径 > ^~ > ~/~* > 部分起始路径 > /
生产中的案例
直接匹配网站根会加速Nginx访问处理:
location = / {
......;
}
静态资源配置:
location ^~ /static/ {
......;
}
# 或者
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
......;
}
多应用配置
location ~* /app1 {
......;
}
location ~* /app2 {
......;
}