标签: rewrite

nginx重写不起作用?它只是忽略它

我对 nginx 有一个奇怪的问题,它不想重写...

我有这个配置,我需要将一个哈希(40 个字符)传递给一个 php 文件,它可以与 apache mod_rewrite 一起使用,但是对于 nginx,我什至没有尝试进行简单的重写,它根本不起作用

server {
.........
        location / {
            rewrite ^aa$ /downloadTORRENTZ.php break;
            root   /usr/share/nginx/html;
            index  index.html index.htm;
            rewrite  "^([A-Z0-9]{40})$" /file.php?ddl=$1 break;
        }
}
Run Code Online (Sandbox Code Playgroud)

rewrite configuration nginx

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

使用 Apache 将 http://example.com 重定向到 http://example.com/mylink 的简单方法?

刚刚开始尝试了解所有指令和选项在 Apache 下的工作方式。

我想对我的一个站点进行重定向(仅在服务器上运行一个站点),以便当请求进入 http:/example.com 时,服务器会自动将它们重定向到http://的子 URL example.com/mylink

我尝试将重定向放入位于 /etc/apache2/sites-enabled 的文件中来重写它,但是顶级域 URL 抱怨它没有正确重定向。

我想我想要的是浏览器重定向,并认为使用

RewriteEngine On
RewriteRule ^/$ /mylink [L,R]
Run Code Online (Sandbox Code Playgroud)

会起作用,但将其放入 .htaccess 文件不起作用(它重定向但立即给出了 500 内部服务器错误。)

尝试重新启动 Apache 时,将其放入 /etc/apache2/sites-enabled 中的文件会导致配置错误。

我知道这很简单……但我错过了什么?

mod-rewrite rewrite redirect apache-2.2

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

将 apache 重写转换为 nginx 重写

我有一个项目,我必须将所有未找到的文件重定向到 index.php。我在 apache 中做了这个,在我的项目文件夹中放置了一个 .htaccess 文件。文件的内容是——

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Run Code Online (Sandbox Code Playgroud)

现在我想在 nginx 中使用同样的方法。这是我的 nginx.conf 的样子

root   /usr/local/apache2/htdocs;
index  index.php index.html index.htm;
location /project/ {
            root   /usr/local/apache2/htdocs;
            index  index.php index.html index.htm;
            try_files $uri $uri/ index.php;
}
Run Code Online (Sandbox Code Playgroud)

现在每当我提出这样的请求时,http://localhost/project/hello这个请求应该去,http://localhost/project/index.php但这说File not found

现在为什么我认为这会起作用是因为由于rootlocation 块内的指令覆盖root了块外的指令(尽管值相同),重写模块将查找/usr/local/apache2/htdocs/project/index.php. 我在这里缺少什么?

更新

这个配置有效

root   /usr/local/apache2/htdocs;
location /project/ {
            index  index.php index.html index.htm;    #effectively root here is /usr/local/apache2/htdocs
            try_files $uri $uri/ /project/index.php;
 } …
Run Code Online (Sandbox Code Playgroud)

rewrite nginx

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

尝试使用 Nginx try_files 来模拟 Apache MultiViews

我想要一个http://example.com/foobar返回的请求http://example.com/foobar.jpg。(或 .gif、.html、.whatever)

这与 Apache MultiViews 无关,在 Nginx 中似乎同样容易。这个问题似乎暗示它会像try_files $uri $uri/ index.php;在 location 块中一样简单,但这不起作用。

try_files $uri $uri/ =404;不起作用,try_files $uri =404;或者try_files $uri.* =404;在我的location / {块和匹配图像的正则表达式之间移动它也不起作用。

至关重要的是,try_files $uri.jpg =404; 确实有效,但仅适用于 .jpg 文件,如果我在一个位置块中使用多个 try_files 规则,则会引发配置错误!

当前server {区块:

server {
        listen   80;
        server_name     example.org www.example.org;
        access_log  /var/log/nginx/vhosts.access.log;
        root   /srv/www/vhosts/example;
        location / {
                root   /srv/www/vhosts/example;
        }

        location ~* \.(?:ico|css|js|gif|jpe?g|es|png)$ {
                expires max;
                add_header Cache-Control public;
                try_files $uri …
Run Code Online (Sandbox Code Playgroud)

rewrite nginx

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

Nginx 重写以消除“www”不起作用

我查看了许多SE 线程并进行了各种谷歌搜索,但无法弄清楚为什么我无法重定向www.mysite.commysite.com我的 nginx 服务器。

第一个服务器块按照您的预期执行http://mysite.info->https://mysite.info重定向。所以我不确定为什么第二个服务器块对www.mysite.info->没有做同样的事情mysite.info

这是我的 nginx.conf 文件的相关部分:

server {
    server_name mysite.info;
    rewrite ^ https://$server_name$request_uri? permanent;
}

server {
    server_name www.mysite.info;
    rewrite ^ https://mysite.info$request_uri? permanent;
}

server {
    listen   443;
    ssl    on;
    server_name mysite.info;
    # other directives, handling PHP, etc.
}
Run Code Online (Sandbox Code Playgroud)

关于出了什么问题的任何想法?

rewrite configuration nginx redirect

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

nginx:禁止访问文件夹,某些子文件夹除外

如何拒绝对文件夹的访问,但“拒绝”中的某些子文件夹除外?

我尝试了这样的事情(按此顺序):

#这个子文件夹不应该被拒绝,里面的php脚本应该是可执行的

位置 ~ /data/public { 允许所有; }

#此文件夹包含许多应拒绝公共访问的子文件夹

位置 ~ /data { 全部拒绝;返回404;}

...这不能正常工作。/data/public 文件夹中的文件是可以访问的(/data 中的所有其他文件都应该被拒绝),但 PHP 文件不再在 /data/public 文件夹中执行(如果我不添加这些限制,PHP文件是可执行的)。

怎么了?怎么可能是正确的?我认为有更好的方法来做到这一点。

如果有人能帮我解决这个问题,那就太好了:)。


谢谢,但 PHP 文件仍然没有在 /data/public/ 文件夹中执行,就像一个简单的

<? echo "test"; ?>
Run Code Online (Sandbox Code Playgroud)

它为您提供此文件作为下载(没有上面的“拒绝”配置,php 文件运行良好)。

我的PHP配置:

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
Run Code Online (Sandbox Code Playgroud)

/data/ 之外的所有其他目录中的 PHP 文件都在工作......(其他子文件夹也是如此)。

php rewrite nginx rules

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

nginx 只将特定的服务器名重写为 https

这是关于两个子域。第一个 (www) 应该通过 http 访问。第二个(云)应该通过 https 访问。

这些是我的条目的相关部分:

server {
        listen 80;
        server_name cloud.example.de;
        rewrite ^ https://$server_name$request_uri? permanent;  # enforce https
}

server {
        listen 443 ssl;
        server_name cloud.example.de;
        root /home/user/web/cloud;
}

server {
        listen 80;
        server_name www.example.de;
        root /home/user/web/cms;

        #etc.
}
Run Code Online (Sandbox Code Playgroud)

当我现在调用http://cloud.example.de 时,我被重定向到https://cloud.example.de,很好。但是当我调用http://www.example.de 时,我也被重定向到https://www.example.de,这会将我带到 cloud.example.com 的内容,因为这是唯一设置为的服务器名称由端口 443 使用

。 www 子域的访问日志中没有条目。
还有另一个指向 phpPgAdmin 的子域。我可以正常访问它,它不会被重写。

server {
        listen 80;
        server_name pgsql.example.de;
        root /home/user/web/phppgadmin;

        #etc
}
Run Code Online (Sandbox Code Playgroud)

有什么不见了?仅当服务器名称匹配 cloud.example.de 时才应进行重写
服务器条目的顺序是否相关或无关紧要?


在 …

rewrite nginx https ubuntu-11.04

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

一起使用 nginx try_files 和 autoindex

我有以下服务器配置:

server {
    listen 80;
    server_name _;

    root /var/www/;

    location /calendars/ {
        autoindex on;
        try_files $uri.ics $uri =404;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果希望autoindex在访问时获得页面http://example.com/calendars/,但是我得到一个404 File not found错误。

我希望服务器做这样的伪代码:

if($uri is directory) {
    if(one of index pages exists in directory) {
        show index page;
    } else {
        show autoindex page;
    }
} else {
    if($uri.ics exists) {
        show $uri.ics;
    } else if($uri exists) {
        show $uri;
    } else {
        show 404 page;
    }
}
Run Code Online (Sandbox Code Playgroud)

rewrite nginx directoryindex

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

OpenLiteSpeed 是否支持 Apache 重写?

我收到了一些相互矛盾的信息 - 官方 OpenLiteSpeed 页面都说:

  1. OpenLiteSpeed 支持 Apache 重写,并且
  2. OpenLiteSpeed 不支持 .htaccess

有点困惑...... LiteSpeed Tech 试图在这里拉什么?

rewrite litespeed apache-2.2

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

如果存在,则 Nginx 提供纯文件,否则提供 /index.php

我希望 Nginx 自行处理对静态文件的任何请求,但如果该文件不存在,则提供 index.php 将处理所有这些请求

目前我的配置是这样的,

server {
listen 80;
listen [::]:80;

root /home/www/example.com/htdocs;

index index.php;

server_name www.example.com;


location ~* ^[^\?\&]+\.(html|jpg|jpeg|json|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|svg|woff|ttf)$ {
    # First attempt to serve request as file, then
    # as directory, then fall back to index.php
    try_files $uri $uri/ /index.php;
    #try_files /favicon.ico =404;
}


location / {
    add_header X-Is-PHP true;
            try_files /index.php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
    }


}
Run Code Online (Sandbox Code Playgroud)

这是我所能得到的,它为任何对静态文件的请求提供服务,如果它不存在,则将 index.php 作为纯文本文件提供。如何将 index.php 传递给 PHP 解释器?

rewrite nginx

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