当前位置: 首页 > 工具软件 > Yourls > 使用案例 >

开源短链接服务yourls配置,nginx代理二级目录配置

龚俊捷
2023-12-01

短链服务 yourls

https://github.com/YOURLS/YOURLS/wiki/Nginx-configuration

安装

https://github.com/YOURLS/YOURLS/releases
  • 将安装包解压并上传至服务器; (比如: /app/yourls)
  • 将 user/config-sample.php 重命名为 user/config.php;
  • 编辑 user/config.php 文件,填入数据库信息和配置站点选项;
  • 访问 http://yoursite.com/admin/ 开始使用吧!
  • api 比较简单 @see http://yourls.org/#API
  • http://yourls.org/
  • http://yourls.org/#FAQ
user/config.php
<?php
/* This is a sample config file.
 * Edit this file with your own settings and save it as "config.php"
 *
 * IMPORTANT: edit and save this file as plain ASCII text, using a text editor, for instance TextEdit on Mac OS or
 * Notepad on Windows. Make sure there is no character before the opening <?php at the beginning of this file.
 */

/*
 ** MySQL settings - You can get this info from your web host
 */

/** MySQL database username */
define( 'YOURLS_DB_USER', 'root' );

/** MySQL database password */
define( 'YOURLS_DB_PASS', 'Bxxxxxx' );

/** The name of the database for YOURLS */
define( 'YOURLS_DB_NAME', 'yourls' );

/** MySQL hostname.
 ** If using a non standard port, specify it like 'hostname:port', eg. 'localhost:9999' or '127.0.0.1:666' */
define( 'YOURLS_DB_HOST', '127.0.0.1:3306' );

/** MySQL tables prefix */
define( 'YOURLS_DB_PREFIX', 'yourls_' );

/*
 ** Site options
 */

/** YOURLS installation URL -- all lowercase, no trailing slash at the end.
 ** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa) 
 * 这里配置的是能访问的网站地址,短链会接到后边进行访问,如果是nginx启动,ng中需要配置相关上下文
 */
define( 'YOURLS_SITE', 'http://127.0.0.1:8020/path/' );  

/** Server timezone GMT offset */
define( 'YOURLS_HOURS_OFFSET', 8 ); 

/** YOURLS language
 ** Change this setting to use a translation file for your language, instead of the default English.
 ** That translation file (a .mo file) must be installed in the user/language directory.
 ** See http://yourls.org/translations for more information
    汉化 到上边的网站下载相关的 .mo 文件
 */
define( 'YOURLS_LANG', 'zh_CN' ); 

/** Allow multiple short URLs for a same long URL
 ** Set to true to have only one pair of shortURL/longURL (default YOURLS behavior)
 ** Set to false to allow multiple short URLs pointing to the same long URL (bit.ly behavior) */
define( 'YOURLS_UNIQUE_URLS', true );

/** Private means the Admin area will be protected with login/pass as defined below.
 ** Set to false for public usage (eg on a restricted intranet or for test setups)
 ** Read http://yourls.org/privatepublic for more details if you're unsure */
define( 'YOURLS_PRIVATE', true );

/** A random secret hash used to encrypt cookies. You don't have to remember it, make it long and complicated. Hint: copy from http://yourls.org/cookie **/
define( 'YOURLS_COOKIEKEY', '1qw12Sw1' );

/** Username(s) and password(s) allowed to access the site. Passwords either in plain text or as encrypted hashes
 ** YOURLS will auto encrypt plain text passwords in this file
 ** Read http://yourls.org/userpassword for more information */
$yourls_user_passwords = array(
	'lijiaqi' => 'lijiaqi',
	// 'username2' => 'password2',
	// You can have one or more 'login'=>'password' lines
	);

/** Debug mode to output some internal information
 ** Default is false for live site. Enable when coding or before submitting a new issue */
define( 'YOURLS_DEBUG', false );
	
/*
 ** URL Shortening settings
 */

/** URL shortening method: 36 or 62 
 62比36增加了大写英文字母
*/
define( 'YOURLS_URL_CONVERT', 62 );
/*
 * 36: generates all lowercase keywords (ie: 13jkm)
 * 62: generates mixed case keywords (ie: 13jKm or 13JKm)
 * Stick to one setting. It's best not to change after you've started creating links.
 */

/** 
* Reserved keywords (so that generated URLs won't match them)
* Define here negative, unwanted or potentially misleading keywords.
*/
$yourls_reserved_URL = array(
	'porn', 'faggot', 'sex', 'nigger', 'fuck', 'cunt', 'dick','tydic','10010','unicom',
);

/*
 ** Personal settings would go after here.
 */
nginx.conf
server
    {
        listen 8020 default_server reuseport;
        #listen [::]:80 default_server ipv6only=on;
        server_name _short;
        index index.html index.htm index.php;
        root  /app/yourls;

        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include enable-php.conf;
	    location /path/ {
			alias /app/yourls;
			try_files $uri $uri/ /yourls-loader.php?$args;
		}
	
     
        access_log  /home/wwwlogs/access-shor.log;
    }

注意:

  • 解压后注意文件夹权限,yourls需要在部署的根目录生成 .htaccess 文件,如果权限不足会导致无法创建,但是可以手动创建 @see https://github.com/YOURLS/YOURLS/wiki/.htaccess ,文件注意描述的是 url是否带路径,会影响这个文件的配置
  • 部署初期可以通过将数据库中的3个表删除达到重新安装的目的
  • 最好开启插件 Random ShortURLs Settings
  • nginx配置的 .htaccess 并没有作用可以不用配置

几组配置片段

1、根路径访问
    #config.php
        define( 'YOURLS_SITE', 'http://127.0.0.1:8020/' );
    #nginx
        server
        {
            listen 8020 default_server reuseport;
            server_name _short;
            index index.html index.htm index.php;
            root  /app/yourls;
            include enable-php.conf;
    	   location / {
    			try_files $uri $uri/ /yourls-loader.php?$args;
    		}
            access_log  /home/wwwlogs/access-shor.log;
        }
    #.htaccess
        # BEGIN YOURLS
        <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^.*$ /yourls-loader.php [L]
        </IfModule>
        # END YOURLS
2、非根路径 (方式不是很好,还是占用了跟路径,下边会有更优解)
    * 这种方式还需要访问根路径下的admin才能进入配置页面, 但生成的短域名携带path
    #config.php
        define( 'YOURLS_SITE', 'http://127.0.0.1:8020/path/' );
    #nginx
        server
        {
            listen 8020 default_server reuseport;
            server_name _short;
            index index.html index.htm index.php;
            root  /app/yourls;
            include enable-php.conf;
    	    location /path {
    	        alias /app/yourls;
    			try_files $uri $uri/ /yourls-loader.php?$args;
    		}
            access_log  /home/wwwlogs/access-shor.log;
        }
    #.htaccess (使用nginx测试发现不用更改)
        # BEGIN YOURLS
        <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /path/
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^.*$ /path/yourls-loader.php [L]
        </IfModule>
        # END YOURLS

FAQ

  1. 短连接生成后访问404

    如果是nginx配置的:需要开发rewrite模式

       location /path {
    		alias /app/yourls;
    		try_files $uri $uri/ /yourls-loader.php?$args;
    	}
    
  2. nginx如果配置了上下文无法访问到yourls的管理端,即想要yourls不占用非根路径,又想功能正常怎么办?

    将根路径设置到yourls工程的上一级,比如我的yourls工程路径是 /app/yourls/ ,我将root设置为 /app ,通过http://127.0.0.1:8020/yourls/admin/ 访问管理端

server
   {
      listen 8020 default_server reuseport;
      server_name _short;
      index index.html index.htm index.php;
      root  /app;
      include enable-php.conf;

      #如果 /path 下不增加 alias /app/yourls/;  打开页面会异常
      # /path 不要与工程文件夹同名,否则访问不到
      location /path {
         alias /app/yourls/;
         try_files $uri $uri/ /yourls/yourls-loader.php?$args; 
         include enable-php.conf;
	  }  
      access_log  /home/wwwlogs/access-shor.log;
   }
define( 'YOURLS_SITE', 'http://127.0.0.1:8020/path' );

nginx/conf/enable-php.conf 修改如下:

    location ~ [^/]\.php(/|$)
    {
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        include pathinfo.conf;
       fastcgi_param SCRIPT_FILENAME $request_filename;
    }
  • 注意:以上方式配置完成后观察在翻页是地址栏会变化

总之,YOURLS_SITE 的路径需要nginx 有同样的配置;比如

define( 'YOURLS_SITE', 'http://127.0.0.1:8020/yourls/path' );

location /yourls/path {
         alias /app/yourls/;
         #try_files 对应的配置还是相对根路径
         try_files $uri $uri/ /yourls/yourls-loader.php?$args; 
	  } 
 类似资料: