【问题标题】:NGINX Rewrite to strip a URL SegmentNGINX 重写以剥离 URL 段
【发布时间】:2015-07-07 19:40:51
【问题描述】:

我已将我的站点从 apache 移至运行 nginx 的新服务器。

旧的网站结构是:

example.com/NEW/

新结构:

example.com/

我有一些应用程序仍然引用上传/文件,例如:

example.com/NEW/assets/images/7871862618261826.jpg

我需要重写所有包含 /NEW/ 参数的请求。

例如:

example.com/NEW/assets/images/7871862618261826.jpg

应该被路由到:

example.com/assets/images/7871862618261826.jpg

我当前的 Nginx 配置如下:

server {
    listen 80;
    server_name www.domain.com domain.com;
    return 301  https://www.domain.com$request_uri;
}

服务器{

listen  443;
if ($http_host = domain.com) {
    rewrite  (.*)  https://www.domain.com$1;
}

location /NEW/ {

     rewrite ^/NEW(.*)$ $1 last;
     #return 405;
     #return 301  https://www.domain.com$request_uri;
}


server_name domain.com www.domain.com;

access_log /var/log/nginx/domaincom.access.log;
error_log /var/log/nginx/domaincom.error.log;

root /var/www/domain.com/public_html/;
index index.html index.htm index.php;

 #set default location
 location / {

        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:8080;

 }

}

如何实现重写?

【问题讨论】:

  • 你的例子应该这样做。问题是什么?
  • 嗨,Alexey,它不是用上面的块重写的。
  • 从定位还是重写?还是两者兼而有之?
  • 我已经添加了完整的 nginx 配置。仍然没有让重写工作。可能与反向代理有关吗?
  • 此配置应按预期重写。您能否更准确地指出哪些是行不通的?

标签: nginx url-rewriting rewrite


【解决方案1】:

我终于可以使用以下位置块:

location ~ /NEW/.*\.(png|jpg|gif|pdf|doc)$ {

     rewrite ^/NEW(.*)$ $1 last;

}

似乎没有按要求执行位置块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2014-05-06
    • 2021-12-22
    • 1970-01-01
    • 2012-06-29
    • 2011-10-17
    相关资源
    最近更新 更多