社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  NGINX

位置匹配后nginx重写

Simon55 • 6 年前 • 919 次点击  

我对nginx很陌生,只是想做一些我认为应该很简单的事情。如果我这样做了:

卷曲 http://localhost:8008/12345678

我希望返回index.html页面。但我却找不到404。/usr/share/nginx/html/12345678没有这样的文件

如果我卷曲 http://localhost:8008/ 我希望请求被传送到 http://someotherplace/ 但是我找到了302,就这样。

对基本问题表示歉意,但希望能得到一些建议!

这是代码:

server {
    listen 8008;
    server_name default_server;

    location / {
       rewrite ^/$ http://someotherplace/ redirect;
    }

    location ~ "^/[\d]{8}" {
       rewrite ^/$ /index.html;
       root /usr/share/nginx/html;
    }
}
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40088
文章 [ 2 ]  |  最新文章 6 年前
IVO GELOV
Reply   •   1 楼
IVO GELOV    7 年前

请试试这个

server {
    listen 8008;
    server_name default_server;
    root /usr/share/nginx/html;

    location / {
       proxy_pass http://someotherplace/;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
    }

    location ~ "^/[\d]{8}" {
       rewrite ^(.*)$ /index.html break; 
    }
}

这个 proxy_pass 将请求路由到远程目标并返回响应。 而不是 rewrite 你可以用 try_files 正如理查德·史密斯所描述的。

Richard Smith
Reply   •   2 楼
Richard Smith    7 年前

这个 ^/$ 与uri不匹配 /12345678 -它只匹配uri / .

您可以使用:

rewrite ^ /index.html break;

这个 ^ 只是许多匹配任何事物的正则表达式之一。这个 break 后缀导致重写的uri在同一个 location 封锁。见 this document 详细情况。


您可以使用 try_files 指令:

location ~ "^/[\d]{8}" {
    root /usr/share/nginx/html;
    try_files /index.html =404;
}

这个 =404 从句从未达到 index.html 一直存在-但是 试用文件 必须至少有两个参数。见 this document 详细情况。