1 、显示乱码问题
server {
listen 80;
server_name example.com;
root /var/www/example;
location / {
charset utf-8; #一般是在个别的location中加入此项,具体情况具体对待
rewrite .* /index.html break;
}
}
2、index显示列表(一般为企业内部使用)
可在在location server 或 http段中加入
autoindex on;//自动显示目录
autoindex_exact_size off;//人性化方式显示文件大小否则以byte显示
autoindex_localtime on;//按服务器时间显示,否则以gmt时间显示
3、location用法
=:对URI做精确匹配;
location = / {
...
}
~:对URI做正则表达式模式匹配,区分字符大小写;
~*:对URI做正则表达式模式匹配,不区分字符大小写;
^~:对URI的左半部分做匹配检查,不区分字符大小写;
不带符号:匹配起始于此uri的所有的url;
location /lizi {
...
}
例如www.example.com/lizi/xxx/xxx
只要是路径以/lizi开头的都匹配,这种一般用于最后的通用匹配。
注意:匹配优先级:=, ^~, ~/~*,不带符号;
具体匹配方式http://seanlook.com/2015/05/17/nginx-location-rewrite/
3、常用正则(跟Linux上的正则没什么区别)
. : 匹配除换行符以外的任意字符
? : 重复0次或1次
+ : 重复1次或更多次
* : 重复0次或更多次
\d :匹配数字
^ : 匹配字符串的开始
$ : 匹配字符串的结束
{n} : 重复n次
{n,} : 重复n次或更多次
[c] : 匹配单个字符c
[a-z] : 匹配a-z小写字母的任意一个
类似分组():==小括号()之间匹配的内容,可以在后面通过
2表示的是前面第二个()里的内容。正则里面容易让人困惑的是\转义特殊字符。==
4、rewrite实例
http {
# 定义image日志格式
log_format imagelog '[$time_local] ' $image_file ' ' $image_type ' ' $body_bytes_sent ' ' $status;
# 开启重写日志
rewrite_log on;
server {
root /home/www;
location / {
# 重写规则信息
error_log logs/rewrite.log notice;
# 注意这里要用‘’单引号引起来,避免{}
rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4;
# 注意不能在上面这条规则后面加上“last”参数,否则下面的set指令不会执行
set $image_file $3;
set $image_type $4;
}
location /data {
# 指定针对图片的日志格式,来分析图片类型和大小
access_log logs/images.log mian;
root /data/images;
# 应用前面定义的变量。判断首先文件在不在,不在再判断目录在不在,如果还不在就跳转到最后一个url里
try_files /$arg_file /image404.html;
}
location = /image404.html {
# 图片不存在返回特定的信息
return 404 "image not found\n";
}
}
if语句块
- 例子是网上找的,if语句块长用在做单独的限制,如限制访问特定的资源,然后对此类请求做处理,rewire或者deny或者proxy_pass等等。
- 例子中的set语句没怎么用过。也没再去查资料,用的时候再查 = =
if ($http_user_agent ~ MSIE) {proxy_pass
rewrite ^(.*)$ /msie/$1 break;
} //如果UA包含"MSIE",rewrite请求到/msid/目录下
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
set $id $1;
} //如果cookie匹配正则,设置变量$id等于正则引用部分
if ($request_method = POST) {
return 405;
} //如果提交方法为POST,则返回状态405(Method not allowed)。return不能返回301,302
if ($slow) {
limit_rate 10k;
} //限速,$slow可以通过 set 指令设置
if (!-f $request_filename){
break;
proxy_pass http://127.0.0.1;
} //如果请求的文件名不存在,则反向代理到localhost 。这里的break也是停止rewrite检查
if ($args ~ post=140){
rewrite ^ http://example.com/ permanent;
} //如果query string中包含"post=140",永久重定向到example.com
https
- 定义一个新的server,配置如下,必须的配置有listen ,server_name, ssl ,ssl_certificate, ssl_certificate_key,一般配置的时候我都是直接复制,然后改主机名,证书私钥文件,日志路径,root的根目录这几项。
- 如果想让访问80的转到443,可用rewrite语句
listen 443;
server_name agent.t.jlhcar.com;
ssl on;
ssl_certificate, "/usr/local/certificate/xxxx.pem";\\证书
ssl_certificate_key "/usr/local/certificate/xxxx.key";\\私钥
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;\\协议
...
//日志以及root根目录的其他配置
server {
listen 80;
server_name www.example.com
rewrite ^/(.*)$ https://www.example.com/$1;
}
php后端处理(fcgi)
location ~ \.php($|/) {
fastcgi_pass unix:/dev/shm/php-fpm.unix; //最重要的一项,根据实际情况来配置(根据php的配置文件listen的配置来配置)
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
负载均衡和反向代理
- 在http配置段要配置一个upstream
upstream ucart{
server host_ip:80;
server host_ip:80;
}
注意:nginx中不识别_(下划线),否则会出现400错误
- 然后再server配置段,将所需要处理的请求反向代理至后端服务器,在可根据需要和服务器配置情况来定义权重,实现负载均衡
location /ucarapi/ {
proxy_pass http://ucar_t/;
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_set_header Host www.example.com; //头信息,后端服务器根据此来找到特定虚拟主机
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
}
喜欢我写的东西的朋友可以关注一下我的公众号,上面有我的学习资源以及一些其他福利。:Devops部落

网友评论