我已经使用 Nginx 在数字海洋 Droplet 上部署了 medusa js。
当我运行 npm start 并通过 http://localhost:9000/app
访问管理 UI 时,它在本地主机上运行良好
但是当我尝试使用相同的环境变量在服务器上执行相同的操作时,它只会将我重定向回登录页面。
它给出了这样的 401 错误
这是其中一项请求的详细信息
这是我的电动车
DATABASE_TYPE=postgres
DATABASE_URL=postgresql://.....
JWT_SECRET=bVra...
COOKIE_SECRET=J31X...
NPM_CONFIG_PRODUCTION=false
YARN_PRODUCTION=false
NODE_ENV=production
当我尝试获取普通商店API(如https://api.everydayoptics.in/store/products)时,它工作正常:
我在同一域上使用默认管理面板,所以它不应该是 CORS 错误。
这是我的 Nginx 配置,我刚刚保留了根路由(everydayoptics.in 和 www.everydayoptics.in)以暂时显示默认的 Nginx 页面,并使用 API 子域(api.everydayoptics.in)作为后端和管理面板.
server {
listen 80;
listen [::]:80;
server_name everydayoptics.in www.everydayoptics.in api.everydayoptics.in;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.everydayoptics.in;
ssl_certificate /etc/letsencrypt/live/everydayoptics.in/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/everydayoptics.in/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
return 301 https://everydayoptics.in$request_uri;
}
server {
server_name everydayoptics.in;
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/everydayoptics.in/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/everydayoptics.in/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
try_files $uri $uri/ =404;
}
}
server {
server_name api.everydayoptics.in;
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/everydayoptics.in/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/everydayoptics.in/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / {
proxy_pass http://localhost:9000;
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;
}
}
所以,这是一个 Nginx 配置问题,在最后一个服务器块的位置添加以下行解决了我的问题。
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host:443;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-Port 443;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 86400;