简体   繁体   English

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

[英]Nginx Rewrite Short URL server in Subfolder URL's only

Nginx/Php aficionado's Nginx / Php迷

I have a Yourls install running on Nginx which i've got working fine using the following .conf file. 我在Nginx上运行了Yourls安装,使用以下.conf文件可以正常工作。 The redirects work, the database is copied over from an apache install and all is fine. 重定向工作,从apache安装复制数据库,一切正常。

However on the old server I had another simple shortURL service running in a sub directory. 但是在旧服务器上,我在子目录中运行了另一个简单的shortURL服务。 It has lots of short URL's created with the style of url.com/p/0aexvibr 它具有许多以url.com/p/0aexvibr样式创建的短URL

They all run in the subdirectory called "p" and all the links look like that, they are plain files with nothing but a URL on the top line. 它们都在名为“ p”的子目录中运行,所有链接看起来都像这样,它们是纯文件,除了第一行之外只有一个URL。

What I need is for Nginx to redirect urls such as url.com/p/0aexvibr and ignore anything to main the URL that is being redirected by the Yourls rewrite rule below. 我需要Nginx重定向诸如url.com/p/0aexvibr之类的url,并忽略任何内容以维护下面的Yourls重写规则所重定向的URL。 Take them to a php script in /p/ which then opens and redirects the user the URL in the file linked (in this case 0aexvibr contains the url http://google.com ) could someone help set this up? 将它们带到/ p /中的php脚本,然后打开该脚本并将用户重定向到链接文件中的URL(在这种情况下,0aexvibr包含URL http://google.com )可以有人帮助设置吗?

Current conf 当前会议

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;

}

Here is the contents of the show.php 这是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";
}
?>

I don't know how that conf file was working for you , when it comes for the YOURLS redirecting. 我不知道该conf文件如何为您工作,当它用于YOURLS重定向时。 Anyway, here is my suggestion for your question: 无论如何,这是我对您的问题的建议:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM