我使用 NodeJS 运行服务器,我的服务器使用 NGINX
因此,运行 AngularJS 和 Express 应用程序时,我的server
(api) 端可以访问,https://domain.com/api
其中api
有一个目录server
我正在尝试执行以下操作,为节点应用程序提供正常服务并https://domain.com/api
充当https://api/domain.com/
# HTTP - redirect all requests to HTTPS:
server {
listen 80;
return 301 https://$host$request_uri;
}
server {
listen 443;
root /var/www/domain.com/dist;
server_name domain.com www.domain.com;
ssl on;
# Use certificate and key provided by Let's Encrypt:
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
rewrite ^/api/(.*) http://api.domain.com/$1 break;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3002;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
我尝试过不同的rewrite ^/api/(.*) http://api.domain.com/$1 break;
版本
location ^~ /api/ {
rewrite ^/api/(.*) http://api.domain.com/$1 permanent;
}
但当我尝试 API 端点时仍然得到 404
答案1
你需要一个额外的服务器块来定义你想要如何提供服务api.domain.com
。因此,如果 domain.com/api 已经可以工作,你可以添加此服务器块来为 api.domain.com 提供服务:
server {
listen 443 ssl;
server_name api.domain.com;
# include SSL stuff here
location / {
# proxy_pass https://domain.com/api$request_uri;
# or
return 301 https://domain.com/api$request_uri;
}
}