lua 处理字符串分割

local function split(szFullString, szSeparator)
    local nFindStartIndex = 1
    local nSplitIndex = 1
    local nSplitArray = {}
    while true do
       local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
       if not nFindLastIndex then
        nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
        break
       end
       nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
       nFindStartIndex = nFindLastIndex + string.len(szSeparator)
       nSplitIndex = nSplitIndex + 1
    end
    return nSplitArray
end

搭建Nginx + Vue +PHP的前后端开发过程

1、新建一个vue项目

2、安装axios   npm install axios

3、在main.js文件里添加
import axios from 'axios'
Vue.prototype.axios = axios
4、开发过程中必定是跨域名请求,所以在vue.config.json中
module.exports = {
    devServer: {
        proxy: {
            "/api": {
                target: 'http://www.xxx.com',
                secure: false,
                changeOrigin: true
            }
        }
    }
}
5、然后在nginx配置文件中添加域名
server {
  listen 80;
  server_name www.xxx.com;
  default_type text/html;
  charset utf-8;

  root D:/www/xxx/dist;

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

  location /api {
    rewrite ^/(.*) /index.php?$1 last;
  }

  location ~* \.(gif|jpg|jpeg|png|js|css|ico)$
  {
     if (!-f $request_filename) {
         return 404;
     }
  }
 
  location ~ \.php$ {
    internal;
    root D:/www/xxx/public;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}
6、后面就可以开始愉快的开发啦
this.axios.get('/api/test/data', {params: {id: 110}})
.then(res => {res.data})
.catch(error => {console.log(error)})
7、我用的是Yaf框架,所以在RouterPlugin里
public function routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response)
{
    $request_uri = explode('/', ltrim($request->getRequestUri(), '/'), 2);
    if ($request_uri[0] == 'api') {
        $request->setRequestUri('/'.$request_uri[1]);
    }
}

这样就能正确的执行原本的TestController下的dataAction方法啦。

在nginx中x-sendfile解决方案

很多时候网站提供下载文件的时候又需要验证用户的权限,我们可以使用PHP的代码来处理,

$contents = file_get_contents($file);
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="'. basename($file) .'"');
header("Content-Length: ".filesize($file));
echo $content; // 或者 readfile($file);

但是这个就会导致我们的程序需要将文件内容从磁盘经过一个固定的 buffer 去循环读取到内存,再发送给前端 web 服务器,最后才到达用户。当需要下载的文件很大的时候,这种方式将消耗大量内存,甚至引发 php 进程超时或崩溃。
最好的方式是由php来验证权限后再由NGINX来处理文件传输。

location /protected/ {
 internal;
 root  /some/path;
}
internal 表示只能在内部访问,不能通过浏览器访问

php文件

$filePath= '/protected/iso.img';
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="'. basename($file) .'"');
//让Xsendfile发送文件
header('X-Accel-Redirect: '.$filePath);

nginx 配置

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 443;
server_name www.domain.com;
ssl on;
ssl_certificate conf.d/ssl/xxx.pem;
ssl_certificate_key conf.d/ssl/xxx.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location /ssl {
proxy_pass http://127.0.0.1:port;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}

root /home/wwwroot/www.domain.com/public;
index index.php index.html;
gzip on;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~* \.(gif|jpg|jpeg|png|js|css|ico)$
{
if (!-f $request_filename) {
return 404;
}
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
fastcgi_intercept_errors on;
}

server {
listen 80;
server_name www.domain.com;
rewrite ^(.*) https://www.domain.com permanent;
}

nginx-http-concat进行静态资源合并

https://github.com/grasses/nginx-lua-static-merger

我服务器是直接安装openresty

将/lua/lua-nginx_static_merger.lua复制到/usr/local/openresty/nginx/lua/lua-nginx_static_merger.lua

在/usr/local/openresty/nginx/conf/nginx.conf的http段加入

lua_package_path "/usr/local/openresty/nginx/conf/test/?.lua;/usr/local/openresty/lualib/?.lua;;";//多个包用;分隔
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";

在server段下加入

location ~ .*\.(js|css)$ {
root /data/domain/public/static;
set $static_root /data/domain/public/static;
set $cache_root /data/domain/static_cache;
content_by_lua_file "/usr/local/openresty/nginx/lua/lua-nginx_static_merger.lua";
}

其中确保/data/domain/static_cache有写入权限

最后
nginx -t
nginx -s reload

OpenResty安装

源码编译
去http://openresty.org/cn/download.html页面下载

wget https://openresty.org/download/openresty-1.13.6.1.tar.gz
tar -xzvf openresty-1.13.6.1.tar.gz
cd openresty-1.13.6.1/
./configure
make -j2
sudo make install

安装后

cd /usr/local/openresty/nginx/sbin
./nginx start

将nginx添加到PATH变量中

PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH

============================================

运行下面的命令就可以添加OpenResty的仓库
sudo yum install yum-utils
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

sudo yum install -y openresty

sudo yum install -y openresty-resty


列出所有 openresty 仓库里头的软件包:
sudo yum --disablerepo="*" --enablerepo="openresty" list available

复制
cp /usr/local/openresty/nginx/sbin/nginx /usr/local/bin
nginx -v

参考:http://openresty.org/cn/linux-packages.html