我的nginx.conf对于提供静态内容应该是什么样子

时间:2017-12-05 01:45:46

标签: nginx nginx-reverse-proxy

我的nginx.conf中包含以下内容,其他所有内容均为

server {
    listen       8081;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    #location / {
        #root   html;
        #index  index.html index.htm;
    #}

    location = /console {
        proxy_pass http://localhost:7001/main/console;
        proxy_set_header        Host             $host;
        proxy_set_header        X-Real-IP        $remote_addr;
        proxy_set_header        X-Forwarded-For  $proxy_add_x_forwarded_for;
        auth_basic "Admin Area";
        auth_basic_user_file /etc/apache2/.htpasswd;
    }
}

现在,当我启动http://localhost:8081/console时,它成功启动了网页,甚至要求我输入密码。

但此位置/main/resources/org/nett/main/images/jquery-3.1.1.min.js

中有一些静态内容

当我点击它时会抛出404。我的nginx.conf应该在/main/resources/org/nett/main/images/文件夹中提供静态内容?

1 个答案:

答案 0 :(得分:1)

您会注意到静态内容的位置/main/resources...与您的任何位置都不匹配。 。 resources不在/main/console下,因此无法在现有的proxy-pass位置访问它。

重新考虑是否需要更改代理的路径。它可以完成,但需要额外的配置。如果您只是将/重定向到7001:/,那么当/main/console处的数据引用/main/resources时,它也会通过nginx拥有有效的代理路径。

如果您对“工作”的定义意味着/console映射到7001/main/console,则您必须扩展该定义,以解决您希望如何在/main/<anythingelse>下映射请求,其中更多可能尚未被发现。您还必须处理重写应用程序发回的对自身的引用(例如,如果从/main/console请求更多,它将与/main/resources下的内容有相同的问题,因为它,也是,/console不匹配。在代理处的URL重写应该只对正则表达式和http有一定的把握,并且只有对RTFM的真诚和实践的意愿。它很复杂。

顺便提一下,如果您尝试将基本身份验证添加到7001上的任何内容,请确保同时加密您的连接(HTTPS),否则您的凭据在网络上是明文(http://mark-kirby.co.uk/2013/how-to-authenticate-apis-http-basic-vs-http-digest/ )。