我们可以使用 NGINX 作为模板引擎的 webapp

ena*_*tor 4 templates nginx node.js

我对基本的 html 模板 webapp 有要求,例如:

http://localhost:3000/myapp?param1=hello¶m2=John被调用它应该返回如下所示的text/html响应:

<html>
<body>
    <p>Nice to see you John. Platform greets you "hello".</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

名称和问候语是从 param 模板化的。所以模板是这样的:

 <html>
 <body>
     <p>Nice to see you {{param1}}. Platform greets you "{{param2}}".</p>
 </body>
 </html>
Run Code Online (Sandbox Code Playgroud)

我目前使用 express.js 在节点服务器中完成此操作,然后服务器通过 nginx.conf 公开:

server {
    listen 80;
    # server_name example.com;

    location / {
        proxy_pass http://private_ip_address:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道这是否可以通过一些插件或其他配置与裸 nginx 来实现,而无需在 3000 端口上托管节点服务器。

ena*_*tor 11

我能够仅使用 Nginx 来解决这个问题,并使用 OpenResty 的 lua 模块对其进行编程。

https://github.com/openresty/lua-nginx-module在nginx.conf,其中一个可以使用现有的Lua库,如给予能力计划https://github.com/bungle/lua-resty-template用于模板!

myapp.lua:

local template = require("resty.template")
local template_string = ngx.location.capture("/templates/greet.html")
template.render(template_string.body, {
    param1 = ngx.req.get_uri_args()["param1"],
    param2 = ngx.req.get_uri_args()["param2"]
})
Run Code Online (Sandbox Code Playgroud)

问候.html:

<html>
<body>
     <p>Nice to see you {{param1}}. Platform greets you "{{param2}}".</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

nginx.conf:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    root ./;
    server {
        listen 8090;

    location /myapp {
        default_type text/html;
        content_by_lua_file ./lua/myapp.lua;
    }
}
Run Code Online (Sandbox Code Playgroud)

content_by_lua_file 这就是 openresty 的强大之处。

我在这里描述了完整的过程:https : //yogin16.github.io/2018/03/04/nginx-template-engine/

希望有人会发现这很有帮助。