Nginx代理配置哪里有问题?

41 views
Skip to first unread message

kenneth

unread,
Nov 13, 2013, 10:43:26 PM11/13/13
to bea...@googlegroups.com
还是无法访问到Beansdb中的数据
    upstream memcacheds {
        server 127.0.0.1:7900;
        server 127.0.0.1:7901;
        server 127.0.0.1:7902;
        server 127.0.0.1:7903;
    }

     location /beansdb/ {
            set $memcached_key $request_uri;
            add_header X-mem-key  $memcached_key;
            memcached_pass  memcacheds;
            default_type text/html;
        }

大家帮我看看,配置哪里有问题呢?

--

kenneth

unread,
Nov 14, 2013, 10:20:45 AM11/14/13
to bea...@googlegroups.com
我在服务器telnet localhost 7900
get key可以取到数据,但是经过nginx出来就404了
不知道什么原因?


2013/11/14 kenneth <kenne...@gmail.com>

Qiangning Hong

unread,
Dec 2, 2013, 10:17:44 PM12/2/13
to bea...@googlegroups.com
你的 key 都是以 "/beansdb/" 开头的? 


--
Qiangning Hong
http://www.douban.com/people/hongqn/
twitter: @hongqn

kenneth

unread,
Dec 2, 2013, 10:56:34 PM12/2/13
to bea...@googlegroups.com
key不是以/beansdb/开头的,就是一般的id,测试用的数据就是一般的文章id作为key,然后把内容content放到beansdb中。
强宁的意思是不是我这句的key有问题
set $memcached_key $request_uri;
如果我只是一般的id作为key,这里怎么写?
我一会自己再测试下,多谢强宁提点。



2013/12/3 Qiangning Hong <hon...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "BeanDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beandb+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

Qiangning Hong

unread,
Dec 2, 2013, 11:20:35 PM12/2/13
to bea...@googlegroups.com

On Tue, Dec 3, 2013 at 11:56 AM, kenneth <kenne...@gmail.com> wrote:
key不是以/beansdb/开头的,就是一般的id,测试用的数据就是一般的文章id作为key,然后把内容content放到beansdb中。
强宁的意思是不是我这句的key有问题
set $memcached_key $request_uri;
如果我只是一般的id作为key,这里怎么写?
我一会自己再测试下,多谢强宁提点。

可以用 if 搭配正则来获得 request_uri 中的指定部分,大概是这样:

if ( $request_uri ~ /beansdb/(.*) ) {
    set $memcached_key $1;
}

详细的看 nginx if 的文档吧

Davies Liu

unread,
Dec 2, 2013, 11:42:57 PM12/2/13
to beandb
可以直接用 
location ~ ^/beansdb(/.+) {
  set $memcached_key $1;
  .....
}


--
You received this message because you are subscribed to the Google Groups "BeanDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beandb+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
 - Davies

Qiangning Hong

unread,
Dec 3, 2013, 12:32:59 AM12/3/13
to bea...@googlegroups.com

On Tue, Dec 3, 2013 at 12:42 PM, Davies Liu <davie...@gmail.com> wrote:
可以直接用 
location ~ ^/beansdb(/.+) {
  set $memcached_key $1;
  .....
}

是的,在 location 中使用正则可以获得 URL 中的内容。

不过需要注意的是,location 从 prefix 改为 regexp 会微妙的改变 location lookup 的顺序。

kenneth

unread,
Dec 3, 2013, 1:18:52 AM12/3/13
to bea...@googlegroups.com
不知道啥什么原因,实事求是的说,我还是没能配置成功。
http {

    upstream memcacheds {
        server 127.0.0.1:7900;
        server 127.0.0.1:7901;
        server 127.0.0.1:7902;
        server 127.0.0.1:7903;
    }
}
    server {
            listen 80;
            server_name   mvmap.com www.mvmap.com *.mvmap.com;

        location /beansdb(/.+) {
            default_type text/html;
            set $memcached_key $1;


            add_header X-mem-key  $memcached_key;
            memcached_pass  memcacheds;
            memcached_next_upstream error timeout invalid_response;
            memcached_connect_timeout 300ms;
            memcached_read_timeout 3s;
            memcached_send_timeout 1s;
        }
}

http://www.mvmap.com/beansdb/1
tornado拦截到了404,我这个key的value是文章内容,html格式的。
如果我

location ~ ^/beansdb(/.+) {
        set $memcached_key $1;
        .....
}
nginx拦截到404

真伤心,我回头再找找原因。





2013/12/3 Qiangning Hong <hon...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "BeanDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beandb+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Davies Liu

unread,
Dec 3, 2013, 1:42:35 AM12/3/13
to beandb
On Mon, Dec 2, 2013 at 10:18 PM, kenneth <kenne...@gmail.com> wrote:
不知道啥什么原因,实事求是的说,我还是没能配置成功。
http {

    upstream memcacheds {
        server 127.0.0.1:7900;
        server 127.0.0.1:7901;
        server 127.0.0.1:7902;
        server 127.0.0.1:7903;
    }

Nnginx 默认是使用 round-robin 算法来选择后端,请确保任何一个 beansdb 节点都能够返回访问的数据(比如这些是 beanseye 节点的话就没问题)。
 
}
    server {
            listen 80;
            server_name   mvmap.com www.mvmap.com *.mvmap.com;

        location /beansdb(/.+) {
            default_type text/html;
            set $memcached_key $1;
 

            add_header X-mem-key  $memcached_key;
            memcached_pass  memcacheds;
            memcached_next_upstream error timeout invalid_response;
            memcached_connect_timeout 300ms;
            memcached_read_timeout 3s;
            memcached_send_timeout 1s;
        }
}

http://www.mvmap.com/beansdb/1
tornado拦截到了404,我这个key的value是文章内容,html格式的。
如果我

location ~ ^/beansdb(/.+) {
        set $memcached_key $1;
        .....
}
nginx拦截到404

nginx 返回 404, 有可能是请求了 beansdb, 但 beansdb 返回key不存在,就会是 404。
请确保 get /1 访问 上面任何一个节点时是有数据的。

建议在 这个 location 里面增加一个 access_log, 便于分析请求是否落在了这个位置。
 

真伤心,我回头再找找原因。





2013/12/3 Qiangning Hong <hon...@gmail.com>

On Tue, Dec 3, 2013 at 12:42 PM, Davies Liu <davie...@gmail.com> wrote:
可以直接用 
location ~ ^/beansdb(/.+) {
  set $memcached_key $1;
  .....
}

是的,在 location 中使用正则可以获得 URL 中的内容。

不过需要注意的是,location 从 prefix 改为 regexp 会微妙的改变 location lookup 的顺序。


--
Qiangning Hong
http://www.douban.com/people/hongqn/
twitter: @hongqn

--
You received this message because you are subscribed to the Google Groups "BeanDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beandb+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

--
You received this message because you are subscribed to the Google Groups "BeanDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beandb+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
 - Davies

kenneth

unread,
Dec 3, 2013, 2:22:04 AM12/3/13
to bea...@googlegroups.com
谢谢Davies,我想可能是斜杠的问题。
我telnet以后是get 1可以得到数据,而不是/1。可能是这个斜杠的问题。
我再去试试。

内嵌图片 1
beanseye.jpg

Davies Liu

unread,
Dec 3, 2013, 2:38:10 AM12/3/13
to beandb
那应该这样写:

location ~ ^/beansdb/(.+) {
   ...
}

2013/12/2 kenneth <kenne...@gmail.com>



--
 - Davies
beanseye.jpg

kenneth

unread,
Dec 3, 2013, 2:46:41 AM12/3/13
to bea...@googlegroups.com
哈哈,成功了,果然是的。
http://www.mvmap.com/beansdb/1
http://www.mvmap.com/beansdb/2

只是这些文本还原的不行,都成了?????。
怎么变成乱码了?

beanseye.jpg

Davies Liu

unread,
Dec 3, 2013, 3:06:45 AM12/3/13
to beandb
需要在nginx 设置正确的Content-Type,比如 
  1. Content-Type:
    text/html; charset=utf-8

很奇怪你的数据是什么编码,我试了好几种都不对,包括utf-8/gbk等等


2013/12/2 kenneth <kenne...@gmail.com>



--
 - Davies
beanseye.jpg

kenneth

unread,
Dec 3, 2013, 3:47:17 AM12/3/13
to bea...@googlegroups.com
我觉得我set到beansdb中去的时候就错了,因为我get 1得到的数据就有问号,nginx代理出来肯定是乱码。
type(content) 这里是str
db.set(str(row[0]), content)
我是直接查询文章表,将文章的内容存到beansdb中。

是字符集的问题,我得首先保证存进去的时候能是中文,但是现在get 1的value本身就是乱码。
我再测试一下,找找原因。

我的mysql字符集是COLLATE='utf8_general_ci'

 
beanseye.jpg

kenneth

unread,
Dec 3, 2013, 4:58:57 AM12/3/13
to bea...@googlegroups.com
刚才洗澡的时候突然想起来了,原来是我MySQLdb连接数据库的时候没有指定字符集charset="utf8"
现在已经显示正常了。
感谢强宁和Davies的悉心指导,非常感谢!
beanseye.jpg
Reply all
Reply to author
Forward
0 new messages