我已经写了一个NodeJS应用程序,它使用express和express-subdomain包来与域: api.localhost和localhost 。
我试图使用NGINX作为反向代理来处理对应用程序的请求,但是我的子域正在返回完整域的内容。 我有问题搞清楚是什么问题 – 有什么想法?
当我不使用NGINX和HTTPS时,它似乎能够正常工作; 但我希望这两个HTTPS没有购买通配符证书(现在我使用certbot,我认为需要一种方法来定义每个域的不同证书)
我的router.ts文件的router.ts应用程序:
import * as express from 'express'; import * as subdomain from 'express-subdomain'; // See subdomain package import { UserRoutes } from './User'; import { AuthRoutes } from './Auth'; export class Router { static routeAPI(app) { const router = express.Router(); router.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH'); res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization'); next(); }); AuthRoutes.route(router); UserRoutes.route(router); app.use(subdomain('api', router)); // App uses subdomain } }
我的api.localhost网站configuration文件:
server { listen 80; server_name api.localhost; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name api.localhost; access_log /var/log/nginx/api.localhost.log; ssl_certificate /etc/letsencrypt/live/api.localhost/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/api.localhost/privkey.pem; ssl_stapling on; ssl_stapling_verify on; location / { proxy_pass http://localhost:8080; 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; } }
我曾试图:
proxy_pass更改为http://api.localhost:8080 proxy_set_header Host到api.localhost $http_host而不是$host 注意: localhost纯粹用作示例域
似乎在我的server.js静态文件之前我的路线被宣布,所以那些被打到第一。
这表示当web服务器在/时使用web/dist :
this.app.use('', express.static('web/dist'));
这是我的路线之前宣布:
Router.routeAPI(this.app); /** Catch errors */ this.app.use((req, res, next) => { res.status(404); return res.json('Howdy'); }); this.app.use((err, req, res, next) => { if (res.headersSent) { return next(err); } if (err instanceof Response) { return res.status(err.status).json(err.toJSON()); } return res.status(500).json(err); });