在systemD服务中设置CORS (跨源资源共享)不起作用。
我有一个启用CORS (交叉源资源共享)的节点模块,现在我的问题是如何将CORS配置从代码移动到systemd服务?
当前代码中的设置:
private allowCrossDomain(req: express.Request, res: express.Response, next: () => void) {
res.header('Access-Control-Allow-Origin', 'http://localhost:4200');
res.header('Access-Control-Allow-Headers', 'Authorization, Content-Type');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST');
res.header('Access-Control-Allow-Credentials', 'true');
next();
}相反,我想在myService.service中添加CORS,但是下面的设置不起作用:
[unit]
Description=Tool operation BE
After=network.target
[Service]
User=ubuntu
Environment=BACKEND_HOST=backend-ops-model.com
Environment=BACKEN_MODEL_PORT=80
Environment=res.header='Access-Control-Allow-Origin','http://example.com'
WorkingDirectory=/opt/backend-service/operation
ExecStart=/usr/bin/node --experimental-worker /opt/backend-service/operation/node_modules/@gst/operation-service/www.js
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target发布于 2019-09-05 14:21:29
这就是解决问题的方法。在代码中添加CORS选项作为环境变量:
private allowCrossDomain(req: express.Request, res: express.Response, next: () => void) {
res.header('Access-Control-Allow-Origin', process.env.CORS_ORIGIN_HOST || 'http://localhost:4200');
...然后在systemd配置中添加Environment指令:
[unit]
Description=Tool operation BE
After=network.target
[Service]
User=ubuntu
Environment=BACKEND_HOST=backend-ops-model.com
Environment=BACKEN_MODEL_PORT=80
Environment=CORS_ORIGIN_HOST=http://alterntive.com
WorkingDirectory=/opt/backend-service/operation
ExecStart=/usr/bin/node --experimental-worker /opt/backend-service/operation/node_modules/@gst/operation-service/www.js
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target在上面的配置中,如果您不在systemd中提供环境变量,那么localhost:4200将被选中为默认值。希望它能在同样的问题上帮助其他人。
https://unix.stackexchange.com/questions/538894
复制相似问题