{"id":5011,"date":"2020-05-26T18:02:00","date_gmt":"2020-05-26T10:02:00","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=5011"},"modified":"2020-05-26T18:02:00","modified_gmt":"2020-05-26T10:02:00","slug":"lua-nginx-%e5%ae%9e%e7%8e%b0%e5%8a%a8%e6%80%81%e7%94%9f%e6%88%90%e5%9b%be%e7%89%87%e7%bc%a9%e7%95%a5%e5%9b%be%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=5011","title":{"rendered":"Lua + Nginx \u5b9e\u73b0\u52a8\u6001\u751f\u6210\u56fe\u7247\u7f29\u7565\u56fe\u529f\u80fd"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u7f29\u7565\u56fe\u7684\u5b9e\u73b0\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\u3002\u6bd4\u5982\uff0c\u6709\u7684\u76f4\u63a5\u653e\u7f6e\u4e00\u5927\u4e00\u5c0f\u4e24\u79cd\u56fe\uff1b\u6709\u7684\u76f4\u63a5\u9760\u524d\u7aef\u53bb\u7f29\u653e\u56fe\u7247\uff1b\u4e5f\u6709\u7684\u9760\u7b2c\u4e09\u65b9\u5e94\u7528\uff0c\u6bd4\u5982\u4e03\u725b\u4e91\u5b9e\u73b0\u3002\u4f46\u662f\u8fd9\u4e9b\u5b9e\u73b0\u90fd\u4e0d\u592a\u7b26\u5408\u6211\u4eec\u3002\u7531\u4e8e\u76ee\u524d\u6211\u4eec\u7684\u5e94\u7528\u6bd4\u8f83\u7279\u6b8a\uff0c\u60f3\u5728\u81ea\u5efa\u7684\u56fe\u7247\u670d\u52a1\u5668\u4e0a\u63a7\u5236\u662f\u5426\u9700\u8981\u663e\u793a\u7f29\u7565\u56fe\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u501f\u52a9\u4e86 Nginx + GraphicsMagick + Lua \u7684\u5f62\u5f0f\u6765\u751f\u6210\u7f29\u7565\u56fe\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u8fb9\uff0cNginx \u8d1f\u8d23 web \u670d\u52a1\u5668\u7684\u804c\u80fd\uff0cGraphicsMagick \u8d1f\u8d23\u751f\u6210\u7f29\u7565\u56fe\uff0cLua \u8d1f\u8d23\u63a7\u5236\u7f29\u7565\u56fe\u5c3a\u5bf8\u4ee5\u53ca\u8c03\u7528 GraphicsMagick\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u6211\u4eec\u9700\u8981\u5728 Nginx \u4e2d\u6574\u5408 Lua\uff0c\u7531 Lua \u5904\u7406\u54cd\u5e94\u8bf7\u6c42\u3002\u5f53 Nginx \u63a5\u6536\u5230\u56fe\u7247\u8bf7\u6c42\u65f6\u89e3\u6790 url \u83b7\u53d6\u56fe\u7247\u5c3a\u5bf8\u4f20\u5230 Lua \u811a\u672c\uff0cLua \u5219\u8c03\u7528 GraphicsMagick \u751f\u6210\u7f29\u7565\u56fe\uff0c\u7136\u540e Lua \u5c06\u751f\u6210\u7684\u7f29\u7565\u56fe\u8fd4\u56de\u5230\u5ba2\u6237\u7aef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u6211\u8bf4\u4e00\u8bf4\u6211\u7684\u6574\u4e2a\u5b9e\u73b0\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u4f9d\u8d56\u5305\uff01<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yum install -y gcc g++ gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u7740\u5b89\u88c5 Lua \u5373\u65f6\u7f16\u8bd1\u5668\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/openresty\/luajit2.git\n\ncd luajit2\nmake &amp;&amp; make install <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u5b8c\u6210\u540e\uff0c\u4fee\u6539 vim \/etc\/profile\uff0c\u6dfb\u52a0\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export LUAJIT_LIB=\/usr\/local\/lib\nexport LUAJIT_INC=\/usr\/local\/include\/luajit-2.1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e00\u6b65\u662f\u914d\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u8ba9 lua \u53ef\u4ee5\u5728\u4efb\u610f\u4f4d\u7f6e\u6267\u884c\u547d\u4ee4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fdd\u5b58\u540e\u91cd\u65b0\u7f16\u8bd1 source \/etc\/profile\u3002\u6211\u4eec\u518d\u521b\u5efa\u8f6f\u8fde\u63a5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ln -s \/usr\/local\/lib\/libluajit-5.1.so.2 \/lib64\/libluajit-5.1.so.2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\uff0c\u6211\u4eec\u518d\u5b89\u88c5 GraphicsMagick\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget https:\/\/sourceforge.net\/projects\/graphicsmagick\/files\/graphicsmagick\/1.3.33\/GraphicsMagick-1.3.33.tar.gz\n\ntar -zxvf GraphicsMagick-1.3.33.tar.gz\ncd GraphicsMagick-1.3.33\n.\/configure --prefix=\/usr\/local\/GraphicsMagick --enable-shared\nmake &amp;&amp; make install\n\n# \u68c0\u6d4b\u5b89\u88c5\u662f\u5426\u6210\u529f\n\/usr\/local\/GraphicsMagick\/bin\/gm version<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u63d0\u9192\uff1a\u5728\u6267\u884c \u201cgm covert\u201d \u65f6\u5982\u679c\u62a5\u9519\uff1a\u201cNo decode delegate for this image format\u201d \u9700\u8981\u5b89\u88c5\u5bf9\u5e94\u7684\u4f9d\u8d56\u5305\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># jpeg \u76f8\u5173\nwget http:\/\/www.ijg.org\/files\/jpegsrc.v9c.tar.gz\n\n# png \u76f8\u5173\nwget https:\/\/download.sourceforge.net\/libpng\/libpng-1.6.37.tar.gz<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u540c\u6837\u662f\u89e3\u538b\u540e\u6267\u884c make &amp;&amp; make install\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u63a5\u4e0b\u6765\uff0c\u5c31\u662f\u6211\u4eec\u7684\u4e3b\u89d2 Nginx \u7684\u5b89\u88c5\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nginx -V<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff0c\u6709\u4e9b\u673a\u5668\u4e0a\u53ef\u80fd\u5df2\u7ecf\u5b89\u88c5\u4e86 Nginx\uff0c\u5982\u679c\u5df2\u7ecf\u5b89\u88c5\u8fc7\u4e86\uff0c\u5219\u9700\u8981\u770b\u770b\u7248\u672c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>--prefix=\/etc\/nginx --sbin-path=\/usr\/sbin\/nginx --modules-path=\/usr\/lib64\/nginx\/modules --conf-path=\/etc\/nginx\/nginx.conf --error-log-path=\/var\/log\/nginx\/error.log --http-log-path=\/var\/log\/nginx\/access.log --pid-path=\/var\/run\/nginx.pid --lock-path=\/var\/run\/nginx.lock --http-client-body-temp-path=\/var\/cache\/nginx\/client_temp --http-proxy-temp-path=\/var\/cache\/nginx\/proxy_temp --http-fastcgi-temp-path=\/var\/cache\/nginx\/fastcgi_temp --http-uwsgi-temp-path=\/var\/cache\/nginx\/uwsgi_temp --http-scgi-temp-path=\/var\/cache\/nginx\/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u50cf\u4e0a\u9762\u8fd9\u79cd\uff0c\u6211\u4eec\u5c31\u9700\u8981\u91cd\u65b0\u5b89\u88c5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget http:\/\/nginx.org\/download\/nginx-1.16.1.tar.gz\nwget https:\/\/github.com\/simpl\/ngx_devel_kit\/archive\/v0.3.0.tar.gz\nwget https:\/\/github.com\/openresty\/lua-nginx-module\/archive\/v0.10.14.tar.gz\n\ntar -zxvf nginx-1.16.1.tar.gz\ntar -xzf v0.3.0.tar.gz\ntar -xzf v0.10.14.tar.gz\n\ncd nginx-1.16.1\n\n.\/configure \u5df2\u5b89\u88c5\u7684\u6a21\u5757 --add-module=\/root\/ngx_devel_kit-0.3.0 --add-module=\/root\/lua-nginx-module-0.10.14\n\nmake \n\ncp \/usr\/local\/nginx\/sbin\/nginx \/usr\/local\/nginx\/sbin\/nginx.bak\n\ncp .\/objs\/nginx \/usr\/local\/nginx\/sbin\/<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff1a\u4e0d\u8981 make install\uff0c\u7136\u540e\u6211\u4eec\u542f\u52a8 nginx\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u80fd\u8fc7\u6210\u529f\u542f\u52a8\uff0c\u5e76\u8bbf\u95ee\u540e\u51fa\u73b0 Welcome to nginx\uff01\u5219\u8868\u660e\u5b89\u88c5\u6210\u529f\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\u5047\u8bbe\uff0c\u6211\u4eec\u5728 nginx \u7684\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a images \u7684\u76ee\u5f55\uff0c\u5728\u8be5\u76ee\u5f55\u4e2d\u653e\u5165 1.png \u7684\u56fe\u7247\u3002\u5177\u4f53\u7684 nginx.conf \u914d\u7f6e\u4fe1\u606f\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server {\n    listen       80;\n    server_name  localhost;\n\n    #charset koi8-r;\n\n    #access_log  logs\/host.access.log  main;\n\n    root  \/usr\/local\/nginx\/images\/;\n\n    location \/ {\n       root html;\n       index index.html;\n    }\n\n    location ~* \\.(gif|jpg|jpeg|png)$ {\n        root  \/usr\/local\/nginx\/images\/;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u91cd\u542f Nginx \u540e\uff0c\u8981\u4fdd\u8bc1\u539f\u56fe\u53ef\u4ee5\u8bbf\u95ee\uff0c\u5219\u8868\u660e\u914d\u7f6e\u6210\u529f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u6211\u4eec\u63a5\u7740\u6574\u5408 lua \u811a\u672c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server {\n    listen       80;\n    server_name  localhost;\n\n    #charset koi8-r;\n\n    #access_log  logs\/host.access.log  main;\n\n    root  \/usr\/local\/nginx\/images\/;\n\n    location \/ {\n       root html;\n       index index.html;\n    }\n\n    location ~* (.*\\.(jpg|jpeg|gif|png))_(\\d+)x(\\d+)\\.(jpg|jpeg|gif|png)$ {\n        if (!-f $request_filename) {\n                set $request_filepath \/usr\/local\/nginx\/images\/$1;\n                set $width $3;\n                set $height $4;\n                set $ext $5;\n                content_by_lua_file \/usr\/local\/nginx\/lua\/ImageResizer.lua;\n        }\n        # \u6ce8\u610f\uff1a\u6539\u914d\u7f6e\u5fc5\u987b\u653e\u5728 if \u4e4b\u540e\n        root  \/usr\/local\/nginx\/images\/;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fee\u6539 nginx.conf \u540e\uff0c\u540c\u6837\u9700\u8981\u91cd\u542f\uff01\u4e0a\u9762\u7528\u5230\u4e86\u4e00\u4e2a lua \u811a\u672c\uff0c\u5728 \/usr\/local\/nginx\/lua\/ \u76ee\u5f55\u4e0b\u521b\u5efa\u7684 ImageResizer.lua \u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>local command = \"\/usr\/local\/GraphicsMagick\/bin\/gm convert   -auto-orient -strip \" .. ngx.var.request_filepath ..<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\u5728\u539f\u56fe url \u540e\u6dfb\u52a0\u7f29\u7565\u56fe\u5c3a\u5bf8\uff08_600x600.png\uff09\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5230\u5bf9\u5e94\u5c3a\u5bf8\u7684\u7f29\u7565\u56fe\u4e86\u3002\u5bf9\u5e94\u7684\u5728 nginx\/images \u76ee\u5f55\u4e0b\u4f1a\u591a\u51fa\u4e00\u4e2a\u56fe\u7247\u6765\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=5011\">Lua + Nginx \u5b9e\u73b0\u52a8\u6001\u751f\u6210\u56fe\u7247\u7f29\u7565\u56fe\u529f\u80fd<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u7f29\u7565\u56fe\u7684\u5b9e\u73b0\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\u3002\u6bd4\u5982\uff0c\u6709\u7684\u76f4\u63a5\u653e\u7f6e\u4e00\u5927\u4e00\u5c0f\u4e24\u79cd\u56fe\uff1b\u6709\u7684\u76f4\u63a5\u9760\u524d\u7aef\u53bb\u7f29\u653e\u56fe\u7247\uff1b\u4e5f\u6709\u7684\u9760\u7b2c\u4e09\u65b9\u5e94\u7528\uff0c\u6bd4\u5982 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118],"tags":[1972,520,1152,313,1154],"class_list":["post-5011","post","type-post","status-publish","format-standard","hentry","category-ndisdevelop","tag-lua","tag-ng-model","tag-nginx","tag-nginx-tfs"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5011"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5011\/revisions"}],"predecessor-version":[{"id":5012,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5011\/revisions\/5012"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}