【问题标题】:Nginx Rewrite Short URL server in Subfolder URL's onlyNginx 仅在子文件夹 URL 中重写短 URL 服务器
【发布时间】:2014-12-24 20:38:55
【问题描述】:

Nginx/Php 爱好者的

我在 Nginx 上运行了 Yourls 安装,使用以下 .conf 文件可以正常工作。重定向工作,数据库是从 apache 安装复制过来的,一切都很好。

但是,在旧服务器上,我在子目录中运行了另一个简单的 shortURL 服务。它有很多用 url.com/p/0aexvibr

风格创建的短 URL

它们都在名为“p”的子目录中运行,所有链接看起来都是这样,它们是纯文件,只有一个 URL 在顶行。

我需要让 Nginx 重定向诸如 url.com/p/0aexvibr 之类的 url,并忽略任何内容来维护由下面的 Yourls 重写规则重定向的 URL。将它们带到 /p/ 中的 php 脚本,然后打开并重定向用户链接文件中的 URL(在本例中,0aexvibr 包含 URL http://google.com)有人可以帮忙设置吗?

当前配置

server {
listen  80;
server_name url.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /var/www/html/url;
charset     utf-8;

 }

location / {

try_files $uri $uri/ /yourls-loader.php;

          index index.php;

include /etc/nginx/conf.d/php-fpm;

}

 include     /etc/nginx/drop;

}

这里是show.php的内容

<?php
if($_GET['id']){
    $id = addslashes($_GET['id']);
    if(file_exists("urls/$id"))
    { 
        $url = file_get_contents("urls/$id");
        header("location:".$url); 
    } else {
        echo "Error : invalid hash";
    }
} else {
    echo "Error : no hash";
}
?>

【问题讨论】:

  • 就像在other question 中一样,您可以使用 nginx 重写规则将请求重定向到将打开好的 url 的 php 脚本。
  • 问题是,如何?
  • 正如您在other question 中所做的那样,您想看看那里有什么问题吗?
  • 啊,我以为我在另一个问题中做到了,但我没有,它破坏了 Yourls 安装并将一切重定向到 show.php 脚本。我只是想用 /p/ 重定向 URL。
  • 因为你的 nginx 重写规则应该是这样的:rewrite ^p/(.*)$ /dir/show.php?id=$1 last; .

标签: php redirect nginx


【解决方案1】:

当涉及到 YOURLS 重定向时,我不知道该 conf 文件是如何为您工作的。无论如何,这是我对您的问题的建议:

server {
listen  80;
server_name url.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
charset     utf-8;


##the next two locations for the old service which calls the show.php
    location ~ ^/p/(.+\.php)$ {
        alias /var/www/html/url/$1;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $request_filename;
        include fastcgi_params;
    }

    location /p {
        alias /var/www/html/url/;
        try_files $uri $uri/ /p/show.php;
    }

##the next two locations for the yourls configuration, expecting it to be in the root directory

    location ~ ^/(.+\.php)$ {
        alias /var/www/html/url/$1;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $request_filename;
        include fastcgi_params;
    }

    location / {
        alias /var/www/html/url/;
        index index.php;
        try_files $uri $uri/ yourls-loader.php;
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 2016-03-11
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 2023-04-03
    • 2013-03-12
    • 1970-01-01
    相关资源
    最近更新 更多