Nginx配置文件
cd /usr/app/nginx/conf/
vim nginx.conf
Nginx.conf基本结构
// nginx全局块
...
// events块
events {
...
}
// http 块
http {
// http全局块
...
// server块
server {
...
}
// http全局块
...
}
// upstream 块
upstream {
}
Nginx.conf配置文件解析
# 配置nginx的用户组 默认为nobody
#user nobody;
#指定工作进程的个数
#默认是1个,具体可以根据服务器cpu数量进行设置,如果不知道cpu的数量,可以设置为auto
worker_processes auto;
# 配置nginx的错误日志 格式为 log路径 log级别
# error_log 的日志级别为: debug info notice warn error crit alert emerg 紧急由低到高
# error_log的默认日志级别为error,那么就只有紧急程度大于等于error的才会记录在日志
# error_log 的作用域为 main http mail stream server location
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#Nginx运行文件地址
#pid logs/nginx.pid;
#工作模式及连接数上限
events {
worker_connections 1024;
}
http {
# 文件扩展名和文件类型映射表
# mime.types 指text/css,image/jpeg,text/html这样的包含类型。
# 在具mimetypes文件里能看见具体结构有哪些
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"';
// 访问日志配置路径
// main
# access_log logs/access.log main;
# 是否开启0拷贝
sendfile on;
# 是否 #减少网络报文段的数量
#tcp_nopush on;
#keepalive_timeout 0;
# 链接超时时间 默认 65s
keepalive_timeout 65;
# 开始gzip压缩,降低带宽使用和加快传输速度,但增加了CPU的使用
#gzip on;
server {
# 端口号80
listen 80;
# 域名 Ip,一级域名,二级域名配置
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;
# }
#}
}
location匹配规则
# ~ 大小写敏感
# ~* 忽略大小写
# ^~ 只需匹配uri开头
# @ 定义一个命名的 location,在内部定向时使用,例如 error_page
location [ = | ~ | ~* | ^~ ] /uri/ { ... }
location @name { ... }
任意匹配 location / {}
当找不到路由的时候就会走这个路由
server {
listen 80;
server_name www.fu.com; # 域名区分
location / {
root html/fu;
index 404.html;
}
}
只能匹配=号后面的内容。
// 精准匹配
location = / {
root html/fu;
index 404.html;
}
// ~* 忽略大小写
location ~* / {
root html/fu;
index 404.html;
}
// ~大小写敏感
location ~ / {
root html/fu;
index 404.html;
}
最长匹配原则
就是当找不到路由的时候,会按最长路径的路由进行匹配
比如:
location /123 {
root html/fu;
index 404.html;
}
location /1234 {
root html/fu;
index 404.html;
}
#动静分离配置
location ~* \.(gif|css|js|png|jpg|jpeg){
root html/fu;
index 404.html;
}
我们访问1234598734时,没有这个路由则会匹配到1234这个路由,按匹配字符数量最多的进行路由