簡體   English   中英

如何在systemD服務中配置CORS?

[英]how to configure CORS in systemD service?

在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

這是解決問題的方式。 在代碼中添加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。 希望它可以幫助其他人解決同樣的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM