美文网首页
Nginx常见问题(十二)

Nginx常见问题(十二)

作者: andpy | 来源:发表于2018-05-25 09:16 被阅读31次

nginx常见问题

相同server_name 多个虚拟主机优先级访问

#server1
server {
    listen 80
    server_name testserver1 www.applelife.xyz;
    loaction {
        ...
        root /opt/app/code1
    }
}

#server2
 server {
    listen 80
    server_name testserver2 www.applelife.xyz;
    loaction {
        ...
        root /opt/app/code2
    }
}

对于相同的serverName,可以运行,但会出现警告, nginx会优先读取到的配置文件

linux查看文件不同

diff test1.conf test2.conf

location匹配的优先级

= 进行普通字符精确匹配,即使完全匹配 较高
^~ 表示普通匹配,使用前缀匹配
~\~* 表示执行一个正则匹配()

优先级: 完全匹配 > 前缀匹配 > 普通正则匹配

//示例
#完全匹配 http://www.applelife.com/code1/
location = /code1/ {
    rewrite ^(.*)$ /code1/index.html break;
}
#正则匹配 
location ~ /code.* {
    rewrite ^(.*)$ /code2/index.html bradk;
}
#前缀匹配
location ^~code {
    rewrite ^(.*)$ /code3/index.html break;
}

Nginx try_files的使用
按顺序检查文件是否存在,缓存的场景

#先在本地查找该文件 ,没有再加一个/ 的路径下查找
location / {
    try_files $uri $uri/ /index.php;
}

//示例 在目录中没哟找到该文件,将会转到 @java_page location
location / {
    root /opt/app/code/cache;
    try_files $uri @java_page;
}

location @java_page {
    proxy_pass http://127.0.0.1:9090;
}

Nginx的 alias 和 root的区别

root配置

如果有一个请求 http://www.applelife.xyz/request_path/image/cat.png

location /request_path/image/ {
    root /local_path/image/;
}

//实际的请求
/lcoal/_path/iamge/request_path/image/cat.png

alias配置

location /request_path/image/ {
    alias /local_path/image/;
}
//实际请求路径
/local_path/image/cat.png

传递用户的真实的Ip地址
在第一级代理的时候,告诉代理设置一个请求头给传过来,貌似这个有难度。

x-real-ip

nginx常见错误码

nginx:413 Request Entity Too Large?
用户上传文件限制 client_max_body_size

502 bad gateway?
后端服务没有响应,tomcat关闭等

504 Gateway timeout ?
后端服务执行超时等 

相关文章

  • Nginx常见问题(十二)

    nginx常见问题 相同server_name 多个虚拟主机优先级访问 对于相同的serverName,可以运行,...

  • nginx学习教程

    主要文档 Nginx功能概述、为什么选择Nginx、Nginx安装、常见问题(FAQ)、配置符号参考、调试 ngi...

  • day12

    下周:keepalived(高可用软件,和nginx没有关系)nginx常用模块、nginx常见问题 1.什么是高...

  • 高并发下的 Nginx 优化

    我已经谈过一些关于Nginx的常见问题; 其中有一些是关于如何优化Nginx. 很多Nginx新用户是从Apach...

  • CentOS 7 MariaDB安装、卸载、备份、还原、数据目录

    CentOS 7 Nginx安装步骤及常见问题汇总,亲测有效![https://www.jianshu.com/p...

  • CentOS 7 Redis安装、数据目录更改新位置,亲测有效!

    CentOS 7 Nginx安装步骤及常见问题汇总,亲测有效![https://www.jianshu.com/p...

  • nginx常见问题

    转发中有/和没有的区别 记住proxy_pass 地址的最后一个斜杠是关键访问 /netdata/xxx/bbb时...

  • Nginx常见问题

    1)Nginx多Server优先级 在开始处理一个http请求时,nginx会取出header头中的Host变量,...

  • nginx使用备忘

    常用命令 配置 nginx.conf default.conf 常见问题 问题1 配置RN环境后react ant...

  • 学习资源

    Ngnix下载网站:只提供最新版本下载链接Nginx Wiki:文档和例子常见问题Get Start 测试配置文件...

网友评论

      本文标题:Nginx常见问题(十二)

      本文链接:https://www.haomeiwen.com/subject/wrkyjftx.html