【问题标题】:Nginx not returning html templates correctly when connecting via dns [duplicate]通过 dns 连接时 Nginx 未正确返回 html 模板 [重复]
【发布时间】:2016-04-09 22:12:18
【问题描述】:

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

  • 应用是用烧瓶做的
  • “前端”是 nginx
  • uwsgi 正在为 nginx 处理 python

  • 服务器正在 WM 上运行最新的 ubuntu 服务器

  • 可以通过浏览器中的 ip 地址从局域网中的另一台计算机访问应用程序,工作正常,并分配给 nginx 中的端口 80,所以我不必自己输入任何端口。 (烧瓶本身被配置为 localhost:8080 用于测试钱包)。

  • 我无法在我尝试通过指向我的传出 ip 的 dns 进行连接时让服务器回复 502 和 404 错误以外的任何错误,我也无法直接连接到传出ip。但是我在其中一个错误日志中可以看到,似乎服务器处理了我的请求,但是烧瓶“current_user”模块之一出现了一个奇怪的错误。我将在下面转储所有日志。

/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"

new-elf-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

new-elf-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。

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

【问题讨论】:

  • 这个错误很清楚,user.is_authenticated 是一个布尔值,而不是一个方法。
  • 谢谢!我认为我的应用程序单独在 windows 上的 venv 中运行更可悲。

标签: python ubuntu nginx flask uwsgi


【解决方案1】:
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 会返回TrueFlase,但是你调用它,它是一个布尔值。

解决方案很简单,你应该使用这个,没有():

{% if current_user.is_authenticated %}

【讨论】:

  • 哇,谢谢,我怎么看不到.. 竖起大拇指!还有一个问题:我需要在我的项目文件夹中配置 nginx 配置,这样网站就会显示我在导航栏中的标识。它位于 new-elf-lite/app/static/img 并被称为 logotype_black。我还需要对其进行设置,以便它可以使用同一文件夹中的图标“favicon.ico”。提前致谢!
  • 有什么问题?你想设置一个图标吗?在这里查看:Adding a favicon.
  • 不,我已经在我的基本 html 中设置了一个图标,并使用 url_for 生成 /static/img/favicon.ico 的 url 并使用 url_for 生成 /static/ 的标识 url img/logotype_black.png。我现在在 nginx error.log 中看到它正在构建错误的 url。这是因为我的 .config 吗? ""/var/www/new-elf-lite/app/static/static/img/logotype_black.png" 失败" "/static/static/"
  • 问题是 /static 中什么都没有显示,img 和自定义 css 等。
  • 我让它工作了,改变了 /static 路由并重新启动了 nginx。还是谢谢哈哈:)!
猜你喜欢
  • 2014-04-12
  • 2021-06-21
  • 2020-02-25
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多