标签: rewrite

Django/modwsgi 设置中的 URL 重写损坏

我正在尝试将 Django 应用程序设置为使用 modwsgi 在 Apache 下运行。我定义了以下虚拟主机:

<VirtualHost *>
    ServerName www.domain.com
    ServerAlias domain.com

    WSGIScriptAlias / /home/domain/apache/django.wsgi

    <Directory /home/domain/apache>
    Order deny,allow
    Allow from all

    Options -Indexes +FollowSymlinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [QSA,L,R=301]
    </Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

最后的重写规则是为了使 www.domain.com 成为规范的站点名称。但还是有什么问题。当我浏览到 > http://domain.com时,我被重定向到 > http://www.domain.com/django.wsgi/。浏览 > http://www.domain.com效果很好。

我的配置中是否缺少某些内容?谢谢。

更新:我将 RewriteRule 更改为

RewriteRule ^django.wsgi/(.*)$ domain.com/$1 [L,R=301]. 
Run Code Online (Sandbox Code Playgroud)

此更改解决了该问题。如果有人能详细说明为什么 /django.wsgi/ 部分最终出现在 URL 中,我将不胜感激。谢谢!

rewrite django mod-wsgi apache-2.2

5
推荐指数
1
解决办法
1965
查看次数

Nginx 将 *.html 重写为 *.php

我已经设置了我的第一个 Nginx 站点,并且重要的重写都已完成。它们必须是手动的,没有规则可能会处理它,所以我有很多这样的规则来处理可能被索引或书签的旧 URL:

rewrite ^/html/Air_III.html /designers-elements-air permanent;
Run Code Online (Sandbox Code Playgroud)

还有一组奇怪的文件似乎获得了流量,在网站的某些部分我们还没有访问过,所以我只是复制了它们。其中大部分都从旧的 .html 重命名为 .php,其他方面没有改变,但可能有些仍然是 .html。

我正在寻找一个规则,该规则将处理对任何目录中的任何 .html 文件的请求,该文件实际上与该名称的当前文件不匹配,并且将重写为 .php 文件(如果存在)。

在“Apache speak”中,这将是一个redirect permanent而不是重写。我宁愿不追踪所有这些并手工完成!

rewrite nginx

5
推荐指数
1
解决办法
9847
查看次数

URL 重写条件可以比较两个服务器变量吗?

我正在尝试将两个服务器变量作为 URL 重写条件的一部分进行比较。但首先,一些背景...

在 IIS 中,如果您请求http://www.example.com/foo并且foo是一个目录,则 IIS 会将 302“移动对象”重定向http://www.example.com/foo/作为“礼貌重定向”(Source)发送到。

如果您使用 IIS+ARR 作为具有 SSL 卸载的反向代理,后端 IIS 节点接收的请求是通过 http 而不是 https。

结合这两种行为,IIS 的礼貌重定向会丢弃 SSL。礼貌重定向使用与 IIS 收到的请求()相同的方案,在这种情况下是 http 而不是 https。

我想创建一个出站重写规则,将传入 URL 与传出 Location 标头进行比较,并在这种情况下将其从 http 重写为 https:

  • 响应是重定向:{RESPONSE_STATUS}是 302
  • 传入的请求是通过 SSL: {HTTPS}is "on"
  • 传入的 URL 不以斜杠结尾。
  • 传出的 Location 标头以斜杠结尾。

以上都是在前提条件下处理的。棘手的部分是最后一点:

  • 传出 Location 标头的路径与传入 URL 相同,并附加了斜杠。

下面是我到目前为止的代码。前提条件和标头重写都可以正常工作。但是,该条件会导致 500 错误(URL 重写模块错误),大概是因为我{REQUEST_URI}在模式中使用。我尝试将条件分成两个并使用捕获组,但这也不起作用。有任何想法吗?

<rule name="Fix: Courtesy Redirect + SSL Offloading = …
Run Code Online (Sandbox Code Playgroud)

rewrite iis-7.5 arr outboundrules

5
推荐指数
1
解决办法
2330
查看次数

nginx 在 404 上提供备用位置

我正在尝试按如下方式设置 nginx 配置:当收到如下请求时/tile/SteveCountryVic/1/2/3.png

  1. 尝试将其传递到 http://localhost:5005/1/2/3.png
  2. 如果是 404s,则将其传递给另一台服务器作为 /tile/SteveCountryVic/1/2/3.png

这是我的配置,它不太工作:

server {
   listen 80;
   server_name localhost;   error_log  /tmp/nginx.error.log notice;
   access_log   /tmp/nginx.access.log;
   location /tile/SteveCountryVic/ {
        rewrite_log on;        
        #rewrite ^.*/(\d+)/(\d+)/(\d+).*$ /$1/$2/$3.png break;

        proxy_intercept_errors on;
        error_page 404 = @dynamiccycletour;        
        #proxy_set_header Host $http_host;
        #proxy_pass http://127.0.0.1:5005;
        proxy_redirect /tile/SteveCountryVic/ http://localhost:5005/;

   location @dynamiccycletour {
        rewrite_log on;
        #rewrite ^(\d+)/(\d+)/(\d+).*$ /tile/SteveCountryVic/$1/$2/$3.png break;
        proxy_pass http://115.x.x.x:20008;


   }

   location /tile/ {
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:20008;


        proxy_cache my-cache;
        proxy_cache_valid  200 302  60m;
        proxy_cache_valid  404      1m;
    }
    ...
Run Code Online (Sandbox Code Playgroud)

在此配置中,所有请求似乎都被重定向到代理服务器,但最终会提供图像。此外,错误日志包含以下几行:

2013/09/10 09:44:11 [error] …
Run Code Online (Sandbox Code Playgroud)

rewrite configuration nginx proxy http-status-code-404

5
推荐指数
1
解决办法
2万
查看次数

如何组织数百万个静态文件以通过网络高效地提供服务?

我想创建一个服务来提供数十万个较小的文件(从 5kb 到 500kb,大部分在 10-100kb 左右)。将其视为一种 gr​​avatar.com,它在https://secure.gravatar.com/avatar/1545f91437e2576b910dbd1023a44756等 URL 上提供这些小头像图片

我想使用没有任何 ID 或哈希值的描述性 URL,例如http://www.server.com/This-is-my-file.ext,没有重复的文件名。

在没有太多开销的情况下提供和组织文件的最有效方法是什么?

只要将所有内容放在一个目录中并让 nginx 提供文件,在达到一定数量的文件后,速度会变慢,具体取决于文件系统。

一个想法是根据文件名的第一个字符将文件保存在一个简单的目录结构中,因此该示例将从 T/h/This-is-my-file.ext 中提供,并在 nginx 配置中使用简单的重写规则。这将导致不同目录的分布非常不均匀。假设使用文件名的 md5 哈希值会产生良好的分布,但需要更多的计算能力...

我想这听起来像是键值存储的完美用例,但是仅仅使用文件系统和 nginx 来保持简单是不是可能?

rewrite filesystems nginx hash

5
推荐指数
1
解决办法
1711
查看次数

Nginx:将 https:// 重写为 http://

我试图让Nginx的传入请求改写到一个网站我的服务器前缀https://http://(这是因为CMS上这个网站的URI的力量,而不是相对路径使用服务-利用图片,脚本和样式表基本上打破了整个网站不可用)。

我目前正在尝试改造由前任制定的现有指令,但我发现自己受限于我对 Nginx 配置语法的了解。这是代码:

if ($ssl_protocol = "") {
    rewrite ^   https://$http_host$request_uri? permanent;
}
Run Code Online (Sandbox Code Playgroud)

我目前已将此代码注释掉以防止强制重写http://to https://,但我无法在不破坏站点的情况下逆转该过程。这是我迄今为止尝试过的:

if ($ssl_protocol = "https") {
    rewrite ^   http://$http_host$request_uri? permanent;
}
Run Code Online (Sandbox Code Playgroud)

# also tried ($ssl_protocol != "")
if (!($ssl_protocol = "")) {
    rewrite ^   http://$http_host$request_uri? permanent;
}
Run Code Online (Sandbox Code Playgroud)

第一个似乎没有影响。发送到客户端的 HTML 仍然会发出未被重写的 HTTPS 请求。

第二个片段似乎也没有任何影响 - 如果我理解正确(尽管我仍然有些惊讶),Nginx 不支持布尔否定。

这样做的最佳方式(或工作方式)是什么?

编辑:我尝试从@NathanC 的答案中添加代码;但是,这种无条件重写会导致重定向循环。仍然感谢帮助。

rewrite nginx

5
推荐指数
1
解决办法
3万
查看次数

在 RewriteCond 中使用 RewriteMap 查找。可能的?

我正在尝试以下

RewriteMap lookup "txt:D:/lookup.txt"
RewriteCond %{REQUEST_URI} ^/${lookup}
RewriteRule ^/(.*)/(.*)$ /a/$1/b/$2.html [PT,L]
Run Code Online (Sandbox Code Playgroud)

我正在尝试比较请求路径是否以有效路径开头。

我在查找文件中有很长的路径列表。

请帮忙解决这个问题。

rewrite redirect apache-2.2 apache-2.4

5
推荐指数
1
解决办法
2105
查看次数

使用 nginx 将请求从 POST 重写为 GET

我有一个后端服务器,由于各种原因,它只处理 GET 请求。该服务器位于 nginx 代理后面(即所有访问都对 nginx 进行,nginx 通过 代理将其发送到后端proxy_pass)。是否可以使 nginx 将 POST 请求重写为 GET 请求,即POST /foo主体内容类型application/x-www-form-urlencoded和主体foo=bar将被代理GET /foo?foo=bar

rewrite http nginx

5
推荐指数
1
解决办法
2万
查看次数

url重写,nginx抛出404错误,错误日志中没有任何内容

下面的重写工作正常:

rewrite ^/city/restaurants$ /city/listings/restaurants permanent;
Run Code Online (Sandbox Code Playgroud)

但这不起作用

rewrite ^/city/restaurants$ /city/listings/restaurants last;
rewrite ^/city/restaurants$ /city/listings/restaurants break;
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

位于整个 nginx 服务器块下方

  server {
          listen 80 default backlog=1024;
          server_name mydomain.com;


          client_max_body_size 20M;
          charset utf-8;
          keepalive_timeout 50;

          access_log /var/log/access_log main;
          error_log /var/log/error_log info;


          root /var/www/;
          index index.php index.phtml index.html;
  autoindex on;

   location ~ \..*/*\.php$ {
       return 403;
   }
   location ~^/sites/.*/private/{
       return 403;
   }
   location ~^/sites/.*/files/* {
       try_files $uri @rewrite;
   }
   location ~ (^|/)\. {
       return 403;
   }

   location / {

rewrite ^/city/restaurants$ /city/listings/restaurants last;

    location …
Run Code Online (Sandbox Code Playgroud)

mod-rewrite rewrite nginx regex http-status-code-404

5
推荐指数
1
解决办法
1万
查看次数

如何根据标题参数动态更改文件名 - nginx 重写

我正在.pdf使用 nginx Web 服务器提供文件。并.pdf使用重写规则设置文件的文件名,具体取决于title args.

我想要做的是,example.com在文件名中添加我的域名

1. 如果titlevar 包含example.com在其中,则使用titlevar 数据作为文件名。

例如

URL : http://example.com/pdf/0D2E8C9DC2F732A6ACC6150811515F697E3C376C.pdf?title=[example.com]ebook
Filename : [example.com]ebook.pdf
Run Code Online (Sandbox Code Playgroud)

2. 如果titlevar 不包含example.com在其中,则将文件名设为[example.com]title var.pdf

例如

URL : http://example.com/pdf/0D2E8C9DC2F732A6ACC6150811515F697E3C376C.pdf?title=ebook
Filename : [example.com]ebook.pdf
Run Code Online (Sandbox Code Playgroud)

3. 如果没有设置标题,则使用文件名作为[example.com]hash.pdf文件名。

例如

URL : http://example.com/pdf/0D2E8C9DC2F732A6ACC6150811515F697E3C376C.pdf
Filename : [example.com]0D2E8C9DC2F732A6ACC6150811515F697E3C376C.pdf
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现这样的目标。?


我目前的配置是这样的

#pdf download block
location ~* /pdf/([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F]+)\.pdf$ {

#if title is not set then use hash.pdf as filename
if ( $arg_title …
Run Code Online (Sandbox Code Playgroud)

rewrite nginx

5
推荐指数
1
解决办法
5126
查看次数