【问题标题】:delivery of static content inside NGINX proxy在 NGINX 代理中传递静态内容
【发布时间】:2016-10-24 18:06:47
【问题描述】:

我正在使用 NGINX 网络服务器来代理/绕过基于特定 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 定义块并重新定义了项目/页面 root 路径,以便任何 $uri被通过 - 但不幸的是它不会。我为这些静态页面所做的每个 css/js/img uri 调用都会强制提供第一个登录页面,而不是相关文件。所以我必须以完整的 url 形式为这些文件添加我的外部链接引用,以“很好”地显示我的静态页面。谁能帮我解决这个问题?

更新

我尝试使用以下配置,同样的问题 - 不会提供 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 reverse-proxy


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢基南!我会尝试您的解决方案,并在完成测试后立即向您发送反馈:)
    • 好的,请告诉我。 P.S感谢您对您的问题的如此详细的描述:)
    • 有效!我为我的图像、css 和 js 文件定义了一个特殊的“资产”子文件夹,并在我的站点配置中放置了一个特殊的位置定义块,更改了我的静态 html 文件中的路径结构,一切正常!
    • ...太好了!!!但是如果我想添加更多的子页面,我必须始终根据您的建议设置一个特殊的位置别名(如 asset.0asset.2 等)。可以直接在我的传入请求位置 URI 上绑定这些静态文件的资产别名定义,或者可以在我的主要位置定义部分内?
    • 太棒了!预期的结果。您不必设置单独的别名本身。一般来说,子页面应该继承自基本页面。因此,它本质上与您的其他静态文件具有相同的根目录。如果不是这种情况,您可以将子页面的资产定义在主文件夹的子文件夹中,只要在 URL 中指定即可。
    猜你喜欢
    • 1970-01-01
    • 2013-03-20
    • 2011-09-17
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多