nginx代理通过快递路线

时间:2018-07-18 16:26:14

标签: express nginx nginx-reverse-proxy

如果使用以下配置和代码,请转到https://domain.co/api/yo

它在快速应用中查找“ / api / yo”路线,而不仅仅是“ / yo”

如何配置NGINX,以便express将https://domain.co/api/yo视为'/ yo'?

server {
    listen 443 default_server;
    listen [::]:443 default_server;
    root /var/www/domain.co;
    index index.html;
    server_name domain.co www.domain.co;
    ssl on;
    ssl_certificate /etc/letsencrypt/live/domain.co/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.co/privkey.pem;

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

    location /api {
        proxy_pass http://127.0.0.1:8080;
    }
}

app.js

const express = require('express')
const app = express()

app.get('/yo', (req, res) => res.send('Hello World!'))

app.listen(8080, () => console.log('Example app listening on port 8080!'))

2 个答案:

答案 0 :(得分:1)

使用重写

location /api {
    proxy_pass http://127.0.0.1:8080;
    rewrite ^/api/?(.*) /$1 break;
}

答案 1 :(得分:0)

Nginx和Express路线必须与我匹配

nginx: 位置/ api

表达: app.get('/ api',...)