【问题标题】:Pyramid url incorrectly generated, served on nginx+phusion passenger and Mako templatesPyramid url 生成错误,在 nginx+phusion 乘客和 Mako 模板上提供
【发布时间】:2012-05-26 13:45:30
【问题描述】:

遇到涉及 Pyramid 请求 url 的问题,其中 request.static_urlrequest.application_url 是 2 个可识别的嫌疑人。

Pyramid 中生成的请求 url 不知何故包含了我放置的视图 url 在网络主机上,但在本地使用 pserve 可以正常工作。

例如:

config.add_route('signin','/view/signin')

Mako 模板内部

我有:

href="${request.static_url('project:static/blueprint/css/screen.css')}" 

应该显示(使用pserve):

href="http://www.site.com/static/blueprint/css/screen.css"

但它却显示:

href="http://www.site.com/view/signin/static/blueprint/css/screen.css"

另一个例子是首页的网址应该显示:

src = "http://www.site.com/static/img/foo.jpg"

而是显示:

src = "http://www.site.com//static/img/foo.jpg"

我目前在 VPS 服务器上使用 nginx 0.8.53 + Phusion Passenger 2.2.15 运行 Pyramid 1.3 + Mako 模板。

这与request.application_url 相同。在视图代码中我发送了一个字典 (url = request.application_url + '/view/signin')

表单的 url 应该显示:

action="http://www.site.com/view/signin"

而是显示:

action="http://www.site.com/view/signin/view/signin"

我已经复制了http://wiki.pylonshq.com/display/pylonscookbook/Running+Pylons+with+NGINX 上的一些 nginx 设置。

特别是:

    #site runs on Pylons 
    location / {
        include /usr/local/nginx/conf/proxy.conf;
        proxy_pass  http://127.0.0.1:8080;
        proxy_redirect  default; 
    }

和proxy.conf:

    proxy_redirect          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;
    client_max_body_size    10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout   90;
    proxy_send_timeout      90;
    proxy_read_timeout      90;
    proxy_buffer_size       4k;
    proxy_buffers           4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;

我留下的其他东西是我不想碰的。

服务器上的 nginx.conf 看起来像这样。 (我不使用 PHP,但那是我不想接触的东西)。

有人建议将应用程序服务/安装在 /,但我不知道该怎么做。

    server {
            listen <ip>:80;

            server_name site.com www.site.com;

            access_log /<path>/access.log combined;
            error_log /<path>/error.log error;

            root /home/<path>/public;

            index index.html index.htm index.php index.php5;
            include /home/<path>/nginx/site.com/*;

            # No mirrors - using strict redirects
            #if ($http_host != site.com) {
            rewrite ^(.*)$ http://site.com$1 permanent;
            #}

            autoindex on;

            passenger_enabled on;
            passenger_base_uri /;

            # Disallow access to config / VCS data
            location ~* /\.(ht|svn) {
                deny all;
            }

            #site runs on Pylons 
            location / {
                include /<path to conf file>/proxy.conf;
                proxy_pass  http://127.0.0.1:8080;
                proxy_redirect  default; 
            }

            # Statistics
            location /stats/ {
                alias /home/<path>/html/;
                auth_basic "Statistics Area";
                auth_basic_user_file /home/<path>/html/.htpasswd;
            }

            location /doc/analog/ {
                alias /usr/share/analog/;
            }

            # PHPMyAdmin
            rewrite ^/dh_phpmyadmin/([^/]*)/(.*)$ /dh_phpmyadmin/$2;

            location /dh_phpmyadmin/ {
                alias /dh/web/phpmyadmin/;
            }

            location ~ /dh_phpmyadmin/(.+)\.php {
                alias /dh/web/phpmyadmin/;
                fastcgi_param SERVER_PORT 80;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include /dh/nginx/etc/fastcgi_params;
                set $relpath "index.php";
                if ($uri ~ ^/dh_phpmyadmin/(.+)$) {
                    set $relpath $1;
                }
                fastcgi_param SCRIPT_FILENAME /dh/web/phpmyadmin/$relpath;
                fastcgi_pass unix:/home/<path>/.php.sock;
            }

            # PHP
            location ~* \.(php|php5|php4)($|/) {
                fastcgi_param SERVER_PORT 80;
                fastcgi_split_path_info ^(.+\.(?:php|php5|php4))(/.*)$;
                if (!-e $document_root$fastcgi_script_name) {
                    return 404;
                }
                include /dh/nginx/etc/fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_pass unix:/home/<path>/.php.sock;
                #pragma php_launch <path>
            }

        }

【问题讨论】:

    标签: nginx passenger pylons pyramid mako


    【解决方案1】:

    您的网络服务器(乘客?我没用过)似乎没有正确设置环境 (request.environ)。这可能是一个配置选项,但如果您在访问/view/signin 时查看环境的SCRIPT_NAMEPATH_INFO 键,您应该会看到SCRIPT_NAME=''PATH_INFO='/view/signin'。如果不是这种情况,您的应用程序前缀可能在乘客中是错误的。

    【讨论】:

      【解决方案2】:

      您的网站没有使用乘客来服务金字塔。它确实使用了 reverse_proxy。除非有办法在端口 8080 上运行乘客,否则 nginx 配置中的乘客部分是无用的。

      也就是说,你可以看看那里:

      http://kbeezie.com/view/using-python-nginx-passenger/5/

      您必须删除此块才能使其与乘客一起使用:

              location / {
                  include /<path to conf file>/proxy.conf;
                  proxy_pass  http://127.0.0.1:8080;
                  proxy_redirect  default; 
              }
      

      然后您必须创建可调用的 wsgi 函数,该函数将调用金字塔。

      您必须为乘客定义一个根路径:

      root /path/to/app/public/;
      

      然后输入/path/to/app/

      一个名为passenger_wsgi.py 的文件与该文件具有相似的内容

      import sys
      import os
      
      def application(environ, start_response):
          start_response("200 OK", [])
          ret = ["%s: %s\n" % (key, value)
              for key, value in environ.iteritems()]
          return ret
      

      但您必须调用pyramid,而不是它,它是您应用的入口点。

      也就是说,您是否有任何理由使用乘客。使用 uwsgi 之类的东西来部署您的应用程序不是更容易吗?与乘客一起部署一个 ruby​​ 应用程序是有意义的,但我看不出你从使用乘客中得到了什么。

      这是一个很好的教程,你可以跳过大部分部分,因为这里只有 nginx-uwsgi 部分对你有意义。

      http://liangsun.org/2012/02/pyramid-nginx-uwsgi-mysql/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-29
        • 2013-03-28
        • 2012-06-23
        • 1970-01-01
        • 1970-01-01
        • 2011-09-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多