nginx 将静态站点根目录保留在DNS中(反向代理)

goucqfw6  于 2023-10-17  发布在  Nginx
关注(0)|答案(1)|浏览(92)

我有一个小的静态子站点,例如www.example.com/foo/,我使用以下nginx配置:

server {
  listen 443 ssl;
  server_name example.com;

  ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

  location / {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_read_timeout 3600;

    proxy_pass http://example-site/;
  }

  location /foo {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_read_timeout 3600;

    proxy_pass http://example-web-foo/;
  }
}

但是,当我在站点中使用一些基于根的URI时,例如/assets/css/style.css,它会转换为https://example.com/assets/css/style.css,而不是https://example.com/foo/assets/css/style.css
有没有办法在代理中指定根目录?

gkl3eglg

gkl3eglg1#

我认为你可以通过使用proxy_set_header指令来编辑X-Forwarded-URI头来指定代理中的根!
看看这个

server {
  listen 443 ssl;
  server_name example.com;

  ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

  location / {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-URI $request_uri; # Add this line
    proxy_read_timeout 3600;

    proxy_pass http://example-site/;
  }

  location /foo {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-URI $request_uri; # Add this line
    proxy_read_timeout 3600;

    proxy_pass http://example-web-foo/;
  }
}

相关问题