通过 dns 连接时 Nginx 未正确返回 html 模板

Nginx not returning html templates correctly when connecting via dns

您好,我为我的组织制作了一个 Web 应用程序,目前我正在将其部署到 ubuntu 服务器上。 Web 应用程序是在 python 中构建的,带有 flask 和一些其他扩展,它在开发中运行良好,但是当我部署它时,我遇到了一些我看不到目的的奇怪错误。

/var/log/uwsgi/new-elf-lite_uwsgi.log 包含 python 错误

[pid: 1565|app: 0|req: 75/75] 192.168.1.1 () {46 vars in 831 bytes} [Tue Jan  5 23:08:17 2016] GET /nyheter => generated 0 bytes in 10 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
Traceback (most recent call last):
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/var/www/new-elf-lite/app/views.py", line 36, in nyheter
    return render_template('news.html', title = title, news = news)
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask/templating.py", line 128, in render_template
    context, ctx.app)
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask/templating.py", line 110, in _render
    rv = template.render(context)
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/jinja2/environment.py", line 989, in render
    return self.environment.handle_exception(exc_info, True)
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/jinja2/environment.py", line 754, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/var/www/new-elf-lite/app/templates/news.html", line 2, in top-level template code
    {% import "bootstrap/wtf.html" as wtf %}
  File "/var/www/new-elf-lite/app/templates/base.html", line 1, in top-level template code
    {% extends "bootstrap/base.html" %}
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 1, in top-level template code
    {% block doc -%}
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 4, in block "doc"
    {%- block html %}
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 20, in block "html"
    {% block body -%}
  File "/var/www/new-elf-lite/venv/local/lib/python2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 21, in block "body"
    {% block navbar %}
  File "/var/www/new-elf-lite/app/templates/base.html", line 71, in block "navbar"
    {% if current_user.is_authenticated() %}
AttributeError: 'bool' object has no attribute '__call__'
[pid: 1565|app: 0|req: 76/76] 192.168.1.1 () {46 vars in 831 bytes} [Tue Jan  5 23:08:18 2016] GET /nyheter => generated 0 bytes in 5 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)

emperor.log 看起来不错

*** Starting uWSGI 2.0.12 (64bit) on [Tue Jan  5 22:53:15 2016] ***
compiled with version: 5.2.1 20151010 on 05 January 2016 20:57:28
os: Linux-4.2.0-16-generic #19-Ubuntu SMP Thu Oct 8 15:35:06 UTC 2015
nodename: ELFSITE
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /
detected binary path: /var/www/new-elf-lite/venv/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
setgid() to 33
setuid() to 33
your processes number limit is 3846
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
Python version: 2.7.10 (default, Oct 14 2015, 16:09:02)  [GCC 5.2.1 20151010]
*** starting uWSGI Emperor ***
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1b08b90
your mercy for graceful operations on workers is 60 seconds
*** Operational MODE: no-workers ***
spawned uWSGI master process (pid: 1560)
*** has_emperor mode detected (fd: 6) ***
[uWSGI] getting INI configuration from new-elf-lite_uwsgi.ini
Tue Jan  5 22:53:15 2016 - [emperor] vassal new-elf-lite_uwsgi.ini has been spawned
Tue Jan  5 22:53:15 2016 - [emperor] vassal new-elf-lite_uwsgi.ini is ready to accept requests
Tue Jan  5 22:53:18 2016 - [emperor] vassal new-elf-lite_uwsgi.ini is now loyal
elf@ELFSITE:/var/www/new-elf-lite$

/var/www/nginx/error.log 我认为它告诉 med 在尝试访问目录时失败了

2016/01/05 23:07:49 [error] 1599#0: *42 open() "/var/www/new-elf-lite/app/static/static/img/logotype_black.png" failed (2: No such file or directory), client: 192.168.1.44, server: localhost, request: "GET /static/img/logotype_black.png HTTP/1.1", host: "192.168.1.167", referrer: "http://192.168.1.167/nyheter"
2016/01/05 23:08:17 [error] 1599#0: *38 upstream prematurely closed connection while reading response header from upstream, client: 192.168.1.1, server: localhost, request: "GET /nyheter HTTP/1.1", upstream: "uwsgi://unix:/var/www/new-elf-lite/new-elf-lite_uwsgi.sock:", host: "game.montell.se"
2016/01/05 23:08:17 [error] 1599#0: *38 upstream prematurely closed connection while reading response header from upstream, client: 192.168.1.1, server: localhost, request: "GET /nyheter HTTP/1.1", upstream: "uwsgi://unix:/var/www/new-elf-lite/new-elf-lite_uwsgi.sock:", host: "game.montell.se"
2016/01/05 23:08:18 [error] 1599#0: *38 upstream prematurely closed connection while reading response header from upstream, client: 192.168.1.1, server: localhost, request: "GET /nyheter HTTP/1.1", upstream: "uwsgi://unix:/var/www/new-elf-lite/new-elf-lite_uwsgi.sock:", host: "game.montell.se"

新精灵-lite_uwsgi.ini

[uwsgi]
#application's base folder
base = /var/www/new-elf-lite

#python module to import
app = run
module = %(app)

home = %(base)/venv
pythonpath = %(base)

#socket file's location
socket = /var/www/new-elf-lite/new-elf-lite_uwsgi.sock

#permissions for the socket file
chmod-socket    = 644

#the variable that holds a flask application inside the module imported at line #6
callable = app

#location of log files
logto = /var/log/uwsgi/%n.log

新精灵-lite_nginx.conf

server {
    listen      80;
    server_name localhost;
    charset     utf-8;
    client_max_body_size 750M;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/new-elf-lite/new-elf-lite_uwsgi.sock;
    }
    location /static {
        root /var/www/new-elf-lite/app/static;
    }
    location /templates {
        root /var/www/new-elf-lite/app/templates;
    }
}

应用程序的结构

new-elf-lite/
----run.py
----new-elf-lite_uwsgi.ini
----new-elf-lite_uwsgi.sock
----new-elf-lite_nginx.conf
----app/
--------static/
----------------css/
--------------------style.css
----------------img/
--------------------logotype.png
--------templates/
------------(all non-permission htmls)
------------admin/
----------------(all admin htmls)
------------moderator/
----------------(all moderator htmls)
--------Etc to make the app run

项目文件夹分配给用户www-data:www-data,甚至在.sock上得到chmod 777。

感谢我能得到的所有帮助!

File "/var/www/new-elf-lite/app/templates/base.html", line 71, in block "navbar"
{% if current_user.is_authenticated() %}
AttributeError: 'bool' object has no attribute '__call__'

current_user.is_authenticated会returnTrueFlase,但你调用它,它是一个布尔值。

解决方法很简单,你应该使用这个,不用 ():

{% if current_user.is_authenticated %}