首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Nginx仅在子文件夹URL中重写短URL服务器

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

Stack Overflow用户
提问于 2014-12-03 00:31:39
回答 1查看 1.7K关注 0票数 0

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),有人可以帮助设置吗?

当前会议

代码语言:javascript
运行
复制
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的内容

代码语言:javascript
运行
复制
<?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";
}
?>
EN

回答 1

Stack Overflow用户

发布于 2014-12-24 20:38:56

对于YOURLS重定向,我不知道conf文件是如何为您工作的。无论如何,这是我对你的问题的建议:

代码语言:javascript
运行
复制
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;
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27254299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档