在 NGINX 代理中传递静态内容

delivery of static content inside NGINX proxy

我正在使用 NGINX 网络服务器 proxy/bypass 根据特定 URI 向某些 JAVA 应用程序(atlassian 产品)发出请求,并为对我的域的根调用创建默认登录页面,一切顺利美好的。但是现在我尝试通过我的站点配置中的特定位置定义来传递另一个静态页面,但我不知道如何修复 css/js/image 传递作为此页面中的相对路径定义。

这是我的服务器路径结构:

# 01 default landing page
/var/www/df-default/
    d css
    d js
    d img
    d fonts
    d _external
    f index.html

# 02: external source
/var/www/df-default/_external
    d css
    d js
    d img
    f impressum.html
    f datenschutz.html

这是我当前的 nginx 站点配置:

server {

    listen       80;
    listen       [::]:80;
    listen       443 default ssl;
    server_name  dunkelfrosch.com

    #
    # ... some ssl stuff here ...
    #

    root /var/www/df-default;

    # default landing page
    location / {
        try_files $uri $uri/ /index.html;
    }

    # proxy config for bitbucket application server
    location /go/to/bitbucket {
        # set 20min proxy timeout
        proxy_read_timeout 1600s;
        proxy_send_timeout 1600s;
        proxy_connect_timeout 1600s;

        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_redirect off;

        proxy_pass http://localhost:7990/go/to/bitbucket;
    }

    # external content 01: static imprint page
    # + should available by calling dunkelfrosch.com/show/me/phpugdd/imprint
    # - no css,js or image files will delivered using this config
    # - I've to insert complete url to css/img/js inside my templates
    #   and have to config the location block as shown below to deliver the page
    location /show/me/phpugdd/imprint {
        try_files $uri $uri/ /_external/web/impressum.html;
    }

    # external content 02: static privacy page
    # + should available by calling dunkelfrosch.com/show/me/phpugdd/privacy
    # - no css,js or image files will delivered using this config
    # - I've to insert complete url to css/img/js inside my templates
    #   and have to config the location block as shown below to deliver the page
    location /show/me/phpugdd/privacy {
        try_files $uri $uri/ /_external/web/datenschutz.html;
    }        
}

我认为我刚刚提供了一个 location 定义块并重新定义了 project/page root 路径,以便任何$uri 将通过 - 但不幸的是它不会。我为这些静态页面所做的每个 css/js/img uri 调用都将强制提供第一个登录页面而不是相关文件。因此,我必须以完整的 url 形式为这些文件添加外部 link 引用,以显示我的静态页面 "fine"。谁能帮我解决这个问题?

更新

我尝试使用以下配置,同样的问题 - 没有 css,将提供 js 或图像文件...

location /show/me/phpugdd/privacy {
    root /var/www/df-default/_external/web;
    index datenschutz.html;
    try_files $uri $uri/ /datenschutz.html;
}

location /show/me/phpugdd/imprint {
    root /var/www/df-default/_external/web;
    index impressum.html;
    try_files $uri $uri/ /impressum.html;
}

您真的很接近解决方案了。 NGINX 的优势之一是它非常擅长交付静态内容。然而,这将要求您知道所有静态文件的 URI 的开头,例如 /static/js/private/myscript.js//static/ 开头。如果您知道这一点,那么您可以执行以下操作:

location ^~ /static/ {
    alias /var/www/df-default/static/;
}

当然,这只是推测,因为我不知道您在提供静态文件时使用的 URI。如果它们以 /css//js/ 等开头,它应该被 try_files 指令中的第一个位置块捕获。但是,由于这没有发生,我认为情况并非如此。如果此解决方案不起作用,请发表评论并说明静态资源的 URI 是什么。