使用HTTPS在NGINX上为多个烧瓶应用提供静态文件
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论