我正在使用nginx(通過gunicorn)爲瓶子應用程序提供靜態文件。針對非默認靜態位置的瓶子nginx和靜態文件問題
在默認的靜態文件夾的靜態文件是工作的罰款:
<link rel="stylesheet" href="{{ url_for('static', filename='css/fa/font-awesome.min.css') }}" />
不過對此我想限制訪問的登錄用戶只有我使用的瓶提供的靜態文件夾等靜態文件:
app.register_blueprint(application_view)
application_view = Blueprint('application_view', __name__, static_folder='application_static')
在HTML我這樣,調用靜態文件:
<link rel="stylesheet" href="{{ url_for('application_view.static', filename='css/main.css') }}" />
然後在應用程序/ application_static中我有受限制的靜態文件。這在本地Flask安裝上可以正常工作,但是當我使用Nginx提供的來自/ static文件夾的文件部署到生產計算機時,出現「NetworkError:404 Not Found - website.com/application_static/main.css」。
有關如何配置Ngix來處理此問題的任何想法?
conf.d/mysitename.conf文件:
upstream app_server_wsgiapp {
server localhost:8000 fail_timeout=0;
}
server {
listen 80;
server_name www.mysitename.com;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
server_name www.mysitename.com;
listen 443 ssl;
#other ssl config here
access_log /var/log/nginx/www.mysitename.com.access.log;
error_log /var/log/nginx/www.mysitename.com.error.log info;
keepalive_timeout 5;
# nginx serve up static files and never send to the WSGI server
location /static {
autoindex on;
alias /pathtositeonserver/static;
}
location/{
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server_wsgiapp;
break;
}
}
# this section allows Nginx to reverse proxy for websockets
location /socket.io {
proxy_pass http://app_server_wsgiapp/socket.io;
proxy_redirect off;
proxy_buffering off;
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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
nginx.conf:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
顯示一些nginx配置和應用程序的更多細節。 – iurisilvio
在本地機器上運行燒瓶wsgi服務器還是gunicorn?你還可以發佈你的nginx conf文件嗎? – lesingerouge
@iurisilvio道歉我沒有早點回來,我一直在另一個項目上。我已經添加了conf.d和nginx conf文件。我在我的本地開發環境中運行燒瓶打包服務器。 –