同域名前后端分离项目nginx配置实践

新项目采用前后端分离的方式开发,前后端代码打算分开部署(同机器且同域名),但打算支持后端依然可访问静态资源(nginx配置仅一份)。
搜索nginx配置大部分都通过url前缀进行转发来做前后端分离,不适用目前项目。

说明

前端框架:vue

后端框架:thinkphp6

前端部署目录:/www/project_static

后端部署目录:/www/project

nginx配置方式

apistatic转发到php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
server {
listen 80;
server_name test.aichenk.com;
index index.html index.htm index.php;

set $static_root '/www/project_static';
set $php_root '/www/project/public';
root $static_root;

location ~ \.php$ {
root $php_root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 32 32k;
}

  location / {
try_files $uri $uri/ /index.html;
}

location ^~ /api/ {
root $php_root;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}

location ^~ /static/ {
root $php_root;
access_log off;
}

# 禁用缓存
location = /index.html {
add_header Cache-Control no-cache;
add_header Pragma no-cache;
add_header Expires 0;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
access_log off;
}
}

另外可通过反向代理方式,若第一次判断文件不存在,则发送到另一个服务中,服务中仅关注后端配置。

php下载大文件失败的一次坑

说明

php提供文件的储存和下载,nginx作为web服务器,fpm做解析。

现象

当下载一个5M大小的图片时,总提示下载失败,或下载下来的文件不完整,仅显示部分图像(每次下载不一样)

php下载相关代码

1
2
3
4
5
6
7
8
9
10
11
12
$file = BASE_PATH . '/public/files/IMG_5727.jpg';
$size = filesize($file);
header( "Content-type: application/octet-stream" );
header("Accept-Ranges: bytes");
header("Accept-Length: {$size}");
header("Content-Disposition: attachment; filename=IMG_5727.jpg");
$fp = fopen($file, 'rb');
$readBuffer = 4096;
while (!feof($fp)) {
echo fread($fp, $readBuffer);
}
fclose($fp);

解决

  • 找了一圈后去服务器上查看了nginx日志,确实有报错信息

    1
    2
    3
    2020/05/06 13:09:13 [crit] 1482#0: *23258 open() "/aichenk/soft/nginx/fastcgi_temp/4/17/0000000174" failed (13: Permis
    sion denied) while reading upstream, client: 172.21.0.12, server: 192.168.5.5, request: "GET /temp/t2 HTTP/1.1", upstr
    eam: "fastcgi://127.0.0.1:9001", host: "192.168.5.5:8080"
  • nginx显示权限问题,最终把nginx运行身份与fpm进行统一

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # fpm配置
    [www]
    user = daemon
    group = daemon

    # 查看到fpm运行身份为`daemon`,编辑nginx配置设置身份

    # nginx配置
    #user nobody;
    user daemon;
  • 之前路径归属用户更改

    1
    chown -R daemon /aichenk/soft/nginx/fastcgi_temp/
  • 测试通过

解释

个人分析这次错误其实是数据放到缓冲区才体现,理论上不光是大文件,数据传输量超过一定值也会有这个问题,在搭建环境的时候注意就可以了

vsftpd配置遇到的错误

环境:centos 7

yum安装

yum install -y vsftpd

增加用户

1
2
3
4
# 家目录为/www 并设置nologin
useradd -d /www -s /sbin/nologin /www
# 修改密码
passwd www

配置(/etc/vsftpd/vsftpd.conf)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
anon_upload_enable=NO
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
chown_uploads=YES
xferlog_file=/var/log/xferlog
xferlog_std_format=YES
ascii_upload_enable=YES
ascii_download_enable=YES
chroot_local_user=YES
listen=YES
listen_ipv6=NO

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
allow_writeable_chroot=YES

启动服务

1
2
3
systemctl start vsftpd
# 设置开机启动
systemctl enable vsftpd

遇到错误 - 1

  • 530 Login incorrect.
    2022/02/20220217164011

  • 解决方案
    编辑文件 /etc/pam.d/vsftpd

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #%PAM-1.0S
    session optional pam_keyinit.so force revoke
    auth required pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
    # 注释这行
    #auth required pam_shells.so
    auth include password-auth
    account include password-auth
    session required pam_loginuid.so
    session include password-auth

遇到错误 - 2

  • 500 OOPS: vsftpd: refusing to run with writable root inside chroot()
    2022/02/20220217164112

  • 解决方案
    编辑文件 /etc/vsftpd/vsftpd.conf

    1
    2
    # 增加该行
    allow_writeable_chroot=YES

搭建私有Composer仓库

适用于公司内部进行包管理,在组件化、服务化场景下部分业务代码不方便放到开源平台,可使用内部git服务器,配合composer/satis项目搭建私有composer处理内部依赖,优化代码结构,统一依赖

环境

  • 系统windows 10
  • php版本7.2.17(当前satis要求^7.2.9)
  • git服务gitlab
  • 示例项目: aichenk/open-crypt

步骤

  • 提交项目源码到gitlab

    • 需配置composer.json,步骤本次不作说明
    • 需可以通过http方式访问(gitlab本身支持)
  • 使用composer建立satis项目并加载依赖

    1
    2
    3
    $ composer create-project composer/satis satis --stability=dev --keep-vcs
    $ cd satis
    $ composer install
  • 增加配置文件satis.json(本次建立在项目根目录,可以自由配置)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    {
    "name": "satis",
    "homepage": "http://localhost:8080/",
    "repositories": [
    {
    "type": "vcs",
    "url": "http://localhost/aichenk/open-crypt.git"
    }
    ],
    "config": {
    "secure-http": false
    }
    }
    • homepage表示satis访问地址(web服务器配置,后续用到)
    • repositories中写入git仓库地址
    • secure-http:false表示支持http访问
  • 生成仓库列表及网页文件

    1
    $ php bin/satis build satis.json ./web
  • 配置web访问(本示例使用php自带web服务演示)

    1
    $ php -S 0.0.0.0:8080 -t ./web
  • 打开配置的homepage验证是否成功

2022/02/14859164-0d824bb72ee81a69

使用

  • 修改项目composer.json文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    {
    "repositories": {
    "packagist": {
    "type": "composer",
    "url": "http://localhost:8080/"
    }
    },
    "config": {
    "secure-http": false
    },
    "require": {
    "aichenk/open-crypt": "^1.0"
    }
    }
  • 执行composer install即可

其他说明

  • 项目中添加多个composer源

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    {
    "repositories": [
    {
    "type": "composer",
    "url": "http://localhost:8080"
    },
    {
    "type": "composer",
    "url": "https://packagist.phpcomposer.com"
    }
    ]
    }
  • 不使用satis直接使用gitlab加载包

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    {
    "repositories": [
    {
    "type": "vcs",
    "url": "http://localhost/aichenk/open-crypt.git"
    },
    {
    "type": "composer",
    "url": "https://packagist.phpcomposer.com"
    }
    ]
    }

PHP性能分析之xdebug+webgrind

Xdebug是一个开放源代码的PHP程序调试器,可以用来跟踪,调试和分析PHP程序的运行状况。这里仅说明用来对php运行状态的使用说明。

xdebug安装

  • 运行php -m查看是否已安装xdebug扩展,已安装则跳过当前步骤
  • 源码地址:https://github.com/xdebug/xdebug
  • 编译完成后编辑php.ini文件,添加如下
1
2
3
4
5
6
7
8
9
10
11
12
13
; 引入xdebug扩展
extension=xdebug.so

; 配置xdebug
[xdebug]
; 自动生产分析文件
xdebug.profiler_enable=0
; 启用触发,url添加参数XDEBUG_PROFILE才会生成分析文件
xdebug.profiler_enable_trigger=1
; 分析文件储存路径(默认/tmp)
; xdebug.profiler_output_dir = "/tmp/xdebug"
; 分析文件名称
; xdebug.profiler_output_name=cachegrind.out.%p
  • 重启php-fpmapache以生效

webgrind安装

使用

  • 请求接口,添加参数XDEBUG_PROFILE,会在指定路径中生成一个文件
    2022/02/20220216175641
  • 打开webgrind
    2022/02/20220216175711
  • 选好参数后点击update如下图
    2022/02/20220216175721

参数说明

  • show 90%:要显示耗时比率,最好不要选100%,会卡。相当于显示出比较耗时的方法
  • Invocation Count:被调用执行的次数
  • Total Self Cost:自身耗时
  • Total Inclusive Cost:综合耗时
  • 比例条:蓝色(内置函数),灰色(引入文件),青色(自定义函数),橙色(过程执行所占时间的比例)