nginx常用配置说明
发布时间:2020-12-13 20:52:12 所属栏目:Nginx 来源:网络整理
导读:nginx的主配置(nginx.conf)说明 #worker进程数量worker_processes 1;#错误日志error_log logs/error.log;#进程ID文件pid logs/nginx.pid;#事件区块开始events { #worker进程支持的最大连接数 worker_connections 1024;}#http区块开始http { #nginx支持的媒体
nginx的主配置(nginx.conf)说明 #worker进程数量
worker_processes 1;
#错误日志
error_log logs/error.log;
#进程ID文件
pid logs/nginx.pid;
#事件区块开始
events {
#worker进程支持的最大连接数
worker_connections 1024;
}
#http区块开始
http {
#nginx支持的媒体类型库文件
include mime.types;
#默认的媒体文件
default_type application/octet-stream;
#开启高效传输模式
sendfile on;
#连接超时
keepalive_timeout 65;
#一个server区块开始
server {
#端口号
listen 80;
#服务主机名
server_name localhost;
#编码
charset utf-8;
#location区块
location / {
#站点根目录
root html;
#默认首页文件
index index.html index.htm;
}
#出现对应状态码时,访问50x.html
error_page 500 502 503 504 /50x.html;
#访问50x.html时指定目录为html
location = /50x.html {
root html;
}
}
}
nginx的状态信息功能 location / {
#打开状态信息开关
stub_status on;
access_log off;
allow 127.0.0.1/24;
deny all;
}
nginx错误日志配置 error_log logs/error.log notice;
nginx访问日志配置 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;
#在高并发的网站下,日志配置可以如下 access_log logs/access.log main gzip buffer=32k flush=5s;
location [= | ~ | ~* | ^~] URI {
... ...
}
~用于区分大小写
~*用于不区分大小写
^~进行常规字符串匹配检查后,不做正则表达式的检查
例如: location = / {
#精确匹配/
}
location / {
#所有location不能匹配后的默认匹配
}
location /www/ {
#匹配常规字符串,有正则,优先匹配正则
}
location ^~ /imgs/ {
#匹配常规字符串,不做正则匹配检查
}
location ~* .(gif|jpg|jpeg)$ {
#正则匹配
}
nginx的rewrite配置 rewrite regex replacement [flag];
例如: rewrite ^/(.*) http://www.baidu.com/$1 permanent;
其中$1表示前面小括号匹配的部分。 flag参数说明:
last 本条规则匹配完成后,继续向下匹配新的规则
break 本条规则匹配完即终止
redirect 返回302临时重定向
permanent 返回301永久重定向
上述,last和break用来实现URL重写,redirect和permanet用来实现URL跳转 server {
listen 80;
server_name book.site.com;
location / {
root html/book;
index index.html index.htm;
}
if($http_host ~* "^(.*).site.com$") {
set $domain $1;
rewrite ^(.*) http://www.site.com/$domain/test.html break;
}
}
当我们访问book.site.com时URL重写为www.site.com/book/test.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- nginx – uWSGI / Emperor:UnicodeEncodeError:’ascii
- amazon-web-services – VPC中实例的Amazon AWS通信
- 如何在SSL终端负载均衡器后面设置nginx以重定向到某个主机的
- nginx – 从Google容器引擎转发HTTPS客户端IP
- node.js – 无法使用gzip为nginx中的分块传输编码关闭从No
- django – 未创建uwsgi套接字文件
- php5-fpm nginx google bot =连接由同行重置
- nginx – 无法重启php5-fpm
- nginx/uwsgi服务器的持久内存Python对象
- 如何使用Symfony Process Component中的sudo命令?