一、问题描述
起初测试阶段,短域名映射的是服务器内网IP,本地通过V**连接服务,yourls所有服务均正常运行。
测试通过后,准备上生产,然后将域名映射到外网IP,管理后台正常,但转换的短域名无法正常打开,报错500。
二、排查过程
因内网正常,所以排查方向一直实在server容器上,起初是apache,重新修改了配置,问题依旧。然后更换成nginx,换了几版配置,问题依然无法解决。nginx访问日志输出如下:
代码语言:javascript复制121.137.271.292 - - [09/Nov/2022:18:34:09 0800] "GET /2r HTTP/1.1" 500 5 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36" "-"
使用后内网IP输出如下:
代码语言:javascript复制10.0.10.167 - - [09/Nov/2022:18:13:36 0800] "GET /apple-touch-icon.png HTTP/1.1" 302 5 "-" "com.apple.WebKit.Networking/18614.2.9.1.12 CFNetwork/1399.4 Darwin/22.1.0" "-"
最后,查看PHP日志(tail -f /var/log/php-fpm/www-error.log),发现使用外网IP时,有异常日志输出:
代码语言:javascript复制[09-Nov-2022 10:33:54 UTC] PHP Fatal error: Interface 'JsonSerializable' not found in /var/www/html/includes/vendor/geoip2/geoip2/src/Model/AbstractModel.php on line 8
三、解决
试着安装一下php-json
代码语言:javascript复制yum -y install php-json
再次使用外网IP访问,居然好了!
四、附:安装过程
4.1安装yourls
代码语言:javascript复制git https://github.com/YOURLS/YOURLS.git
配置
代码语言:javascript复制define( 'YOURLS_DB_USER', 'root' );
define( 'YOURLS_DB_PASS', '123456' );
define( 'YOURLS_DB_NAME', 'yourls' );
define( 'YOURLS_DB_HOST', 'localhost' );
define( 'YOURLS_DB_PREFIX', 'yourls_' );
//上面是数据信息不用多说
define( 'YOURLS_SITE', 'http://test.com' ); //你自己服务器的域名 用最短的,短地址也是基于这个生成。
define( 'YOURLS_HOURS_OFFSET', ' 8'); //时区偏移
define( 'YOURLS_LANG', 'zh_CN' ); //这个语言默认是英文,没有中文包,需要自己去 https://github.com/guox/yourls-zh_CN/下载,放到 user/languages 里面
define( 'YOURLS_UNIQUE_URLS', true ); //短地址是否唯一
define( 'YOURLS_PRIVATE', true ); //是否私有,如果私有的,则进行api调用生成短地址时需要传递用户名和密码
define( 'YOURLS_COOKIEKEY', 'A2C7&H~r80pTps{nIfI8VFpTxnfF3c)j@J#{nDUh' );//加密cookie 去 http://yourls.org/cookie 获取
$yourls_user_passwords = array(
'admin' => '123456' /* Password encrypted by YOURLS */ , //用户名=>密码 可填多个 登录成功后这里的明文密码会被加密
);
define( 'YOURLS_DEBUG', false ); //是否开启调试
define( 'YOURLS_URL_CONVERT', 62 ); //使用36进制 还是62进制 这个最好一开始设好不要修改,避免地址冲突,建议62进制
$yourls_reserved_URL = array(
'porn', 'faggot', 'sex', 'nigger', 'fuck', 'cunt', 'dick', //排除一下短地址,这些地址是不会生成的
);
如果目录下没有.htaccess文件,需要添加一个
代码语言:javascript复制# 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
4.2安装nginx
详见《Linux 下 Nginx1.8 安装》
配置
代码语言:javascript复制server {
# HTTP over IPv4 & IPv6
listen 80;
listen [::]:80;
# HTTPS over IPv4 & IPv6
#listen 443 ssl;
#listen [::]:443 ssl;
#ssl_certificate example.com.crt;
#ssl_certificate_key example.com.key;
server_name d.buma.cn;
# Root directory
root /var/www/html;
index index.php;
location / {
try_files $uri $uri/ /yourls-loader.php;
}
location ~ ^/. .php {
fastcgi_index index.php;
fastcgi_split_path_info ^(. .php)(.*)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm/www.sock;
}
}
4.3安装apache(和nginx二选一)
代码语言:javascript复制yum install httpd* -y
配置
代码语言:javascript复制DocumentRoot "/var/www/html"
#
# Relax access to content within /var/www.
#
<Directory "/var/www">
AllowOverride All
# Allow open access:
Require all granted
</Directory>
# Further relax access to the default document root:
<Directory "/var/www/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
4.4验证
管理端:http://host/admin
短域名生成接口:POST http://host/yourls-api.php
参数:
1、signature 签名
2、action 操作类型(固定shorturl)
3、url 要转换的url
4、format 返回数据格式(固定json)