这是我的 nginx 配置:
server {
listen 80;
server_name example.com;
server_tokens off;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
proxy_pass http://frontend:4200;
# return 301 https://$host$request_uri;
}
location /api {
proxy_pass http://backend:8080;
rewrite ^/api/(.*) /$1 break;
}
}
如何创建两者:重定向到 301 https://$host$request_uri; 和 proxy_pass http://frontend:4200;
如果取消注释此字符串 # 返回 301 https://$host$request_uri; 我只看到重定向。
答案1
如果您只想通过 HTTPS 为您的后端提供服务,那么您需要在 HTTPS 服务器块中定义您的后端,例如像这样:
# HTTP server for redirect
server {
listen 80;
server_name example.com;
server_tokens off;
return 301 https://$host$request_uri;
}
# HTTPS server for application
server {
listen 443 ssl http2;
ssl_certificate /path/to/example.com.crt;
ssl_certificate_key /path/to/example.com.key;
server_name example.com;
server_tokens off;
location / {
proxy_pass http://frontend:4200;
}
location /api {
proxy_pass http://backend:8080;
rewrite ^/api/(.*) /$1 break;
}
}