Nginx 仅在子文件夹 URL 中重写短 URL 服务器


Nginx Rewrite Short URL server in Subfolder URL's only

Nginx/Php 爱好者的

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

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

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

我需要的是 Nginx 重定向 url 等 url.com/p/0aexvibr 并忽略任何内容来主控由下面的 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;
}

以下是节目的内容.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";
}
?>

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

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;
    }
}