使用HTTPS在NGINX上为多个烧瓶应用提供静态文件

发布于 2025-02-12 04:02:04 字数 2295 浏览 0 评论 0原文

我正在尝试使用HTTPS(使用certbot)在NGINX上运行多个烧瓶应用程序,但是我无法正确提供静态文件(404)。我对Nginx相对较新,因此我不确定HTTPS反向代理是否引起了问题,否则我认为location指令是正确设置的静态文件。

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1.2;

    ssl_ciphers SECRET_STUFF;
    ssl_prefer_server_ciphers On;
    ssl_session_cache shared:SSL:128m;
    add_header Strict-Transport-Security "max-age=31557600; includeSubDomains";
    ssl_stapling on;
    ssl_stapling_verify on;

    location /app-one/ {
        proxy_pass http://127.0.0.1:9001/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }

    location /app-one/static/ {
        alias /home/pi/flask_test/app_one/static;
    }

    location /app-two/ {
        proxy_pass http://127.0.0.1:9002/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }

    location /app-two/static/ {
        alias /home/pi/flask_test/app_two/static;
    }
}

只是为了澄清,我能够通过/app-One/app-two来查看不同的应用程序,但是静态文件(当前是单个CSS进行测试的文件)不会到达。这是烧瓶应用程序的完整性:

# main.py
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

if __name__ == '__main__':
    app.run()
<!-- templates/index.html -->
<html>
<head>
  <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}" />
</head>
<body>
  <h1>flask app one</h1> <!-- or "flask app two" -->
</body>
</html>
/* static/styles.css */
body {
    background-color: yellow;
}

关于我在这里缺少的内容的任何想法?

I'm trying to run multiple Flask apps on Nginx with HTTPS (using Certbot), but I can't get the static files to be served correctly (404). I'm relatively new to Nginx, so I'm not sure if the HTTPS reverse proxy is causing the issues, cause otherwise I think the location directives are setup correctly for the static files.

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1.2;

    ssl_ciphers SECRET_STUFF;
    ssl_prefer_server_ciphers On;
    ssl_session_cache shared:SSL:128m;
    add_header Strict-Transport-Security "max-age=31557600; includeSubDomains";
    ssl_stapling on;
    ssl_stapling_verify on;

    location /app-one/ {
        proxy_pass http://127.0.0.1:9001/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }

    location /app-one/static/ {
        alias /home/pi/flask_test/app_one/static;
    }

    location /app-two/ {
        proxy_pass http://127.0.0.1:9002/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }

    location /app-two/static/ {
        alias /home/pi/flask_test/app_two/static;
    }
}

And just to clarify, I'm able to view the different apps at /app-one and /app-two, but the static files (which is currently a single CSS file for testing) isn't coming thru. Here's the Flask app for completeness:

# main.py
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

if __name__ == '__main__':
    app.run()
<!-- templates/index.html -->
<html>
<head>
  <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}" />
</head>
<body>
  <h1>flask app one</h1> <!-- or "flask app two" -->
</body>
</html>
/* static/styles.css */
body {
    background-color: yellow;
}

Any ideas on what I'm missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文