使用proxy_pass时,如何让nginx提供与index.html相邻的资产?

时间:2019-01-06 17:51:43

标签: nginx github-pages

使用proxy_pass时,如何让nginx提供与index.html相邻的资产?

上下文:我有一个github存储库,该存储库使用github页面提供内容。从存储库提供服务时,GHP需要与存储库名称

匹配的网址路径

rightisleft.github.io/repo_name/

当前index.html和所有子目录都按预期工作。从(css/*, images/*)开始加载资产会返回200秒。

但是,资源库根目录中的资产robots.txt之类的文件和其他文件返回404。

这是我的域名.conf

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name www.redacted.com;

    # SSL
    ssl_certificate /etc/letsencrypt/live/redacted.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/redacted.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/redacted.com/fullchain.pem;

        location / {
            proxy_set_header Host rightisleft.github.io;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass https://rightisleft.github.io/redacted/;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }        
}

# HTTP redirect
server {
    listen 80;
    listen [::]:80;

    server_name www.redacted.com,redacted.com;

    include nginxconfig.io/letsencrypt.conf;

    location / {
        return 301 https://www.redacted.com$request_uri;
    }
}

# subdomains redirect
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name redacted.com;

    # SSL
    ssl_certificate /etc/letsencrypt/live/redacted.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/redacted.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/redacted.com/fullchain.pem;

    return 301 https://www.redacted.com$request_uri;
}

编辑

Nginx用于证书管理,以将一些不同的微服务捆绑在一起。

1 个答案:

答案 0 :(得分:-1)

尝试一下,因为它可能起作用(如果您的css / assets在github repo页面中),并且您不需要设置许多重定向,除非您有很多域/子域并且我还将redacted.com设置为server_name:

    server {
            listen 80 http2;
            listen [::]:80 http2;
            listen 443 ssl http2 default_server;
            listen [::]:443 ssl http2 default_server;
            server_name redacted.com www.redacted.com;

    # SSL
    ssl_certificate /etc/letsencrypt/live/redacted.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/redacted.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/redacted.com/fullchain.pem;

            location / {
                    proxy_set_header        Host                    $host;
                    proxy_set_header        X-Real-IP               $remote_addr;
                    proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
                    proxy_set_header        X-Forwarded-Proto       $scheme;
                    proxy_pass              https://rightisleft.github.io/redacted$request_uri;
                    proxy_buffering         off;
                    proxy_redirect          default; #or off
                    proxy_intercept_errors on;

                    # allow GitHub to pass caching headers instead of using your own
                    expires off;
            }
    }