python tornado favicon 的設定方法

官方的建議:
http://www.tornadoweb.org/en/stable/guide/running.html

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
    use epoll;
}

http {
    # Enumerate all the Tornado servers here
    upstream frontends {
        server 127.0.0.1:8000;
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
        server 127.0.0.1:8003;
    }

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;

    keepalive_timeout 65;
    proxy_read_timeout 200;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1000;
    gzip_proxied any;
    gzip_types text/plain text/html text/css text/xml
               application/x-javascript application/xml
               application/atom+xml text/javascript;

    # Only retry if there was a communication error, not a timeout
    # on the Tornado server (to avoid propagating "queries of death"
    # to all frontends)
    proxy_next_upstream error;

    server {
        listen 80;

        # Allow file uploads
        client_max_body_size 50M;

        location ^~ /static/ {
            root /var/www;
            if ($query_string) {
                expires max;
            }
        }
        location = /favicon.ico {
            rewrite (.*) /static/favicon.ico;
        }
        location = /robots.txt {
            rewrite (.*) /static/robots.txt;
        }

        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://frontends;
        }
    }
}

網友 1號建議:

You need to wrap favicon.ico with parenthesis and escape the period in the regular expression. Your code will become

favicon_path = '/path/to/favicon.ico'

settings = {
    'debug': True, 
    'static_path': os.path.join(PATH, 'static')}

handlers = [
    (r'/', WebHandler),
    (r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': favicon_path})]

application = tornado.web.Application(handlers, **settings)
application.listen(port)
tornado.ioloop.IOLoop.instance().start()

網友2號建議:

You can simply do something like this:

a. delete static_path from the app settings.

b.

handlers = [
            (r'/favicon.ico', tornado.web.StaticFileHandler, {'path': favicon_path}),
            (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': static_path}),
            (r'/', WebHandler)
]

haven’t tested but I see no reason it shouldn’t work.


官方是一定可以run, 但用起來有點怪怪的,因為當不使用 nginx 時就無法使用 /favicon.ico

我的解法:

(r’/(favicon\.ico)’, MyStaticFileHandler, {‘path’: favicon_path}),

favicon_path 放的是 static 的這一個 folder, 不是指到 /static/favicon.ico 但實際會存取 /static/favicon.ico 這樣真的很神奇。


 

如果是tornado处理全部请求的话的确要求写static_path,或者使用tornado.web.StaticHandler,而一般生产环境下部署的时候,我们一般会在前端部署一个nginx的,可以在这里做静态文件处理,把static目录下的文件让nginx来执行。

server 
{
        listen 80;
        server_name www.abc.com;
        index index.html index.htm index.php;
        root  /storage/www/abc;
        server_name_in_redirect  off;
        access_log  off;
        # Allow file uploads
        client_max_body_size 10M;
        proxy_read_timeout 10;

        location = /favicon.ico {
            rewrite (.*) /static/favicon.ico;
        }
        location = /robots.txt {
            rewrite (.*) /static/robots.txt;
        }

    location ^~ /static/ {
                    root /storage/www/abc;
                    if ($query_string) {
                        expires max;
                    }
                }

        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://abc;
        }
}

当然,开发时为了方便,在tornado里配置一个static目录比较好。


不知道為何,我的nginx 版本好像跟別人的不相容,上面的寫法都無法正確被套用到,我的設定值如下:


upstream myfrontends {
    server 127.0.0.1:5444;
}

server {
    listen       5443;
    server_name  my_server_name;
    charset utf-8;
    ssl on;
    ssl_certificate /etc/nginx/ssl/Server.pem;
    ssl_certificate_key /etc/nginx/ssl/Server.key;

    index index.html index.htm;
    root /my_server_path/static;
    server_name_in_redirect  off;
    access_log  off;

    location = /favicon.ico {
        rewrite (.*) /static/favicon.ico;
    }
    location = /robots.txt {
        rewrite (.*) /static/robots.txt;
    }

    location /static/
    {
        alias /my_server_path/static/;
    }
    location /css/
    {
        alias /my_server_path/static/css/;
    }
    location /js/
    {
        alias /my_server_path/static/js/;
    }
    location /images/
    {
        alias /my_server_path/static/images/;
    }

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_pass http://myfrontends;
    }
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *