trac+nginx报错

56 views
Skip to first unread message

Lord Leatherface

unread,
Jul 24, 2012, 3:04:37 AM7/24/12
to trach...@googlegroups.com
大家好,之前问过这个问题,但没解决,再求助一次

情况是这样的,直接访问/Trac/newticket页面(或者在admin管理页面注销时也一样),显示为502 bad gateway,

nginx报错如下

2012/07/24 14:40:07 [error] 23147#0: *1409542 upstream prematurely closed connection while reading response header from upstream, client: 60.176.175.42, server: 60.191.124.61, request: "GET /Trac/newticket HTTP/1.1", upstream: "uwsgi://unix:///tmp/trac.sock:", host: "60.191.124.61:8000"

uwsgi报错如下

[pid: 22386|app: 0|req: 26/26] 60.176.175.42 () {46 vars in 994 bytes} [Tue Jul 24 14:40:07 2012] GET /Trac/newticket => generated 0 bytes in 137 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
Traceback (most recent call last):
  File "./trac_wsgi.py", line 31, in application
    return dispatch_request(environ, start_request)
  File "/usr/local/lib/python2.6/dist-packages/Trac-0.12.3-py2.6.egg/trac/web/main.py", line 490, in dispatch_request
    return _dispatch_request(req, env, env_error)
  File "/usr/local/lib/python2.6/dist-packages/Trac-0.12.3-py2.6.egg/trac/web/main.py", line 561, in _dispatch_request
    req.send_error(sys.exc_info(), status=e.code, env=env, data=data)
  File "/usr/local/lib/python2.6/dist-packages/Trac-0.12.3-py2.6.egg/trac/web/api.py", line 466, in send_error
    self.write(data)
  File "/usr/local/lib/python2.6/dist-packages/Trac-0.12.3-py2.6.egg/trac/web/api.py", line 531, in write
    self.end_headers()
  File "/usr/local/lib/python2.6/dist-packages/Trac-0.12.3-py2.6.egg/trac/web/api.py", line 320, in end_headers
    self._send_cookie_headers()
  File "/usr/local/lib/python2.6/dist-packages/Trac-0.12.3-py2.6.egg/trac/web/api.py", line 628, in _send_cookie_headers
    for name in self.outcookie.keys():
  File "/usr/local/lib/python2.6/dist-packages/Trac-0.12.3-py2.6.egg/trac/web/main.py", line 522, in _dispatch_request
    dispatcher.dispatch(req)
  File "/usr/local/lib/python2.6/dist-packages/Trac-0.12.3-py2.6.egg/trac/web/main.py", line 285, in dispatch
    raise HTTPForbidden(to_unicode(e))
trac.web.api.HTTPForbidden

nginx的配置如下

server {
listen 8000;
server_name 60.191.124.61;
client_max_body_size 10M;
location /Trac {
include uwsgi_params;
uwsgi_pass unix:///tmp/trac.sock;
uwsgi_param SCRIPT_NAME /Trac;
uwsgi_param REMOTE_USER $remote_user;
uwsgi_modifier1 30;

access_log off;
}

location /Trac/chrome {
alias /opt/Trac/deploy/htdocs/;
autoindex off;
}

location /Trac/row-attachment {
alias /opt/Trac/attachments/;
autoindex off;
}
}

uwsgi命令行如下

uwsgi -s /tmp/trac.sock --chdir /opt/Trac/deploy/cgi-bin -w trac_wsgi -M -t 120 -T -C -d /opt/Trac/logs/uwsgi.log

--
You Suffer,But Why?

Richard Liao

unread,
Jul 24, 2012, 3:23:47 AM7/24/12
to trach...@googlegroups.com
把trac的日志打开看看,应该有相应的错误提示。
> --
> 邮件来自: Google 论坛“TraChinese”论坛。
> 发言: trach...@googlegroups.com
> 退订: trachinese-...@googlegroups.com
> 详细: http://groups.google.com/group/trachinese
> 工程: http://trac-hacks.org/wiki/TracChineseTranslation



--
Richard Liao

Lord Leatherface

unread,
Jul 24, 2012, 3:29:26 AM7/24/12
to trach...@googlegroups.com
log_type=file后,整个trac都不能访问了,直接502 bad gateway

2012/7/24 Richard Liao <richard...@gmail.com>



--
You Suffer,But Why?

Richard Liao

unread,
Jul 24, 2012, 3:40:19 AM7/24/12
to trach...@googlegroups.com
目录的权限问题有没有问题?
tracd跑有没有问题?

Lord Leatherface

unread,
Jul 24, 2012, 4:15:25 AM7/24/12
to trach...@googlegroups.com
权限用root和www-data都一样
tracd正常,会出现

错误: Forbidden

执行该操作需要 TICKET_CREATE 权限


2012/7/24 Richard Liao <richard...@gmail.com>



--
You Suffer,But Why?

Richard Liao

unread,
Jul 24, 2012, 4:26:05 AM7/24/12
to trach...@googlegroups.com
On Tue, Jul 24, 2012 at 4:15 PM, Lord Leatherface <moto...@gmail.com> wrote:
> 权限用root和www-data都一样
trac的日志对查错非常关键,想办法打开吧。
我遇到的情况中,多半都是文件系统权限问题。

> tracd正常,会出现
>
> 错误: Forbidden
>
> 执行该操作需要 TICKET_CREATE 权限
>
可以把TICKET_CREATE 权限赋予anonymous试验一下。

Lord Leatherface

unread,
Jul 24, 2012, 4:27:57 AM7/24/12
to trach...@googlegroups.com
已经确认过就是权限问题引起的,nginx时看不到这个forbidden页面,直接到502了

2012/7/24 Richard Liao <richard...@gmail.com>



--
You Suffer,But Why?

Richard Liao

unread,
Jul 24, 2012, 4:31:52 AM7/24/12
to trach...@googlegroups.com
如果找不到原因,试试修改 trac/main.py +531行:
env.log.warn(exception_to_unicode(e))
把exception_to_unicode(e)输出到别的地方看看。

Lord Leatherface

unread,
Jul 24, 2012, 4:47:48 AM7/24/12
to trach...@googlegroups.com
日志打开了,出现错误时的日志部分如下

2012-07-24 16:45:19,130 Trac[main] WARNING: HTTPNotFound: 404 Not Found (没有可用的管理面板)
2012-07-24 16:45:19,161 Trac[perm] DEBUG: No policy allowed anonymous performing EMAIL_VIEW on None
2012-07-24 16:46:36,718 Trac[main] DEBUG: Dispatching <Request "GET '/newticket'">
2012-07-24 16:46:36,719 Trac[session] DEBUG: Retrieving session for ID '268ee11265ee2ea9f262204d'
2012-07-24 16:46:36,721 Trac[main] DEBUG: Negotiated locale: None -> zh_CN
2012-07-24 16:46:36,732 Trac[api] WARNING: Unable to find repository '(default)' for synchronization
2012-07-24 16:46:36,734 Trac[perm] DEBUG: No policy allowed anonymous performing TICKET_ADMIN on None
2012-07-24 16:46:36,734 Trac[perm] DEBUG: No policy allowed anonymous performing TICKET_CREATE on None
2012-07-24 16:46:36,735 Trac[chrome] DEBUG: Prepare chrome data for request
2012-07-24 16:46:36,737 Trac[perm] DEBUG: No policy allowed anonymous performing ACCTMGR_CONFIG_ADMIN on None
2012-07-24 16:46:36,737 Trac[perm] DEBUG: No policy allowed anonymous performing ACCTMGR_USER_ADMIN on None
2012-07-24 16:46:36,737 Trac[perm] DEBUG: No policy allowed anonymous performing TRAC_ADMIN on None
2012-07-24 16:46:36,737 Trac[perm] DEBUG: No policy allowed anonymous performing PERMISSION_GRANT on None
2012-07-24 16:46:36,738 Trac[perm] DEBUG: No policy allowed anonymous performing PERMISSION_REVOKE on None
2012-07-24 16:46:36,738 Trac[perm] DEBUG: No policy allowed anonymous performing VERSIONCONTROL_ADMIN on None
2012-07-24 16:46:36,743 Trac[main] WARNING: HTTPForbidden: 403 Forbidden (执行该操作需要 TICKET_CREATE 权限)
2012-07-24 16:46:36,744 Trac[perm] DEBUG: No policy allowed anonymous performing EMAIL_VIEW on None

Lord Leatherface

unread,
Jul 24, 2012, 5:50:26 AM7/24/12
to trach...@googlegroups.com
不知道是否和nginx的版本有关,配置一样的测试环境(nginx 1.0.11)下就没这个问题,而出问题的环境nginx 1.0.4

2012/7/24 Lord Leatherface <moto...@gmail.com>



--
You Suffer,But Why?

Richard Liao

unread,
Jul 24, 2012, 6:02:21 AM7/24/12
to trach...@googlegroups.com
看来问题还是出在TICKET_CREATE 权限上, 试试给anonymous加上权限看看。

至于为什么会502,看是不是跟trac_wsgi.py有关。怀疑是异常没有处理好造成的。
File "./trac_wsgi.py", line 31, in application
观察一下trac进程是否挂掉就知道了。

以下是我用的trac.fcgi:

import os
os.environ['PYTHON_EGG_CACHE'] = "/tmp/"
try:
from trac.web import fcgi_frontend
fcgi_frontend.run()
except SystemExit:
raise
except Exception, e:
print 'Content-Type: text/plain\r\n\r\n',
print 'Oops...'
print
print 'Trac detected an internal error:'
print
print e
print
import traceback
import StringIO
tb = StringIO.StringIO()
traceback.print_exc(file=tb)
print tb.getvalue()

Lord Leatherface

unread,
Jul 24, 2012, 6:05:07 AM7/24/12
to trach...@googlegroups.com
不管是TICKET_CREATE权限啊,下面这个是管理员登录时退出的报错,本来也会显示包含“ 没有可用的管理面板 ”文字的页面的

2012-07-24 16:45:19,130 Trac[main] WARNING: HTTPNotFound: 404 Not Found (没有可用的管理面板) 

Lord Leatherface

unread,
Jul 24, 2012, 10:59:36 PM7/24/12
to trach...@googlegroups.com
nginx升级到了1.2.2,问题还是一样,郁闷
--
You Suffer,But Why?

Richard Liao

unread,
Jul 24, 2012, 11:14:10 PM7/24/12
to trach...@googlegroups.com
trac_wsgi.py有没有看?

2012/7/25 Lord Leatherface <moto...@gmail.com>:

Lord Leatherface

unread,
Jul 24, 2012, 11:16:30 PM7/24/12
to trach...@googlegroups.com
trac_wsgi.py就是默认的trac_wsgi的拷贝,没改动过,内容如下
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C)2008-2009 Edgewall Software
# Copyright (C) 2008 Noah Kantrowitz <no...@coderanger.net>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Noah Kantrowitz <no...@coderanger.net>
import os

def application(environ, start_request):
    if not 'trac.env_parent_dir' in environ:
        environ.setdefault('trac.env_path', '/opt/Trac')
    if 'PYTHON_EGG_CACHE' in environ:                                           
        os.environ['PYTHON_EGG_CACHE'] = environ['PYTHON_EGG_CACHE']
    elif 'trac.env_path' in environ:
        os.environ['PYTHON_EGG_CACHE'] = \
            os.path.join(environ['trac.env_path'], '.egg-cache')
    elif 'trac.env_parent_dir' in environ:
        os.environ['PYTHON_EGG_CACHE'] = \
            os.path.join(environ['trac.env_parent_dir'], '.egg-cache')
    from trac.web.main import dispatch_request
    return dispatch_request(environ, start_request)

Lord Leatherface

unread,
Jul 24, 2012, 11:29:12 PM7/24/12
to trach...@googlegroups.com
下面的页面也显示为502,所以不只是新建任务单的问题,而且这类页面都无法显示,不知道是什么原因

错误: Not Found

没有可用的管理面板

TracGuide — Trac用户及管理员指南


2012/7/25 Lord Leatherface <moto...@gmail.com>



--
You Suffer,But Why?

Richard Liao

unread,
Jul 24, 2012, 11:32:14 PM7/24/12
to trach...@googlegroups.com
main.py:

def _dispatch_request(req, env, env_error):
try:
....
except HTTPException, e:
try:
req.send_error(sys.exc_info(), status=e.code, env=env, data=data)
except RequestDone:
pass

在发生HTTPException后,如果req.send_error抛出非RequestDone的异常,
在trac_wsgi的dispatch_request(environ, start_request)中是没有任何异常处理的。
结果应该是trac_wsgi异常退出。因此会发生502。
观察一下python进程是否挂掉就能验证了。

Lord Leatherface

unread,
Jul 24, 2012, 11:37:32 PM7/24/12
to trach...@googlegroups.com
正常情况下也没有python进程啊,是通过uwsgi来加载的

2012/7/25 Richard Liao <richard...@gmail.com>



--
You Suffer,But Why?

Richard Liao

unread,
Jul 25, 2012, 2:17:04 AM7/25/12
to trach...@googlegroups.com
On Wed, Jul 25, 2012 at 11:37 AM, Lord Leatherface <moto...@gmail.com> wrote:
> 正常情况下也没有python进程啊,是通过uwsgi来加载的
>
弄错了。uwsgi是没有python进程的。
不过还是怀疑是异常没有处理导致的问题。
这样加2句看看:
main.py line 560:
try:
req.send_error(sys.exc_info(), status=e.code, env=env, data=data)
except RequestDone:
pass
except Exception, e:
send_internal_error(env, req, sys.exc_info())

except Exception, e:
send_internal_error(env, req, sys.exc_info())

Lord Leatherface

unread,
Jul 25, 2012, 2:24:05 AM7/25/12
to trach...@googlegroups.com
加了也还是一样的
我怀疑是不是这两个页面没有静态化引起的

Richard Liao

unread,
Jul 25, 2012, 2:43:51 AM7/25/12
to trach...@googlegroups.com
晕了,send_internal_error里也有:
req.send_error(exc_info, status=500, env=env, data=data)

2012/7/25 Lord Leatherface <moto...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages