您的位置:首页 > 其它

根据手机类型适配适当页面

2011-05-06 16:46 387 查看
debian下根据手机类型适配适当页面

本文是参考如下2篇文章,debian下在nginx server level根据用户手机支持的协议(xhtml、wml、html)、屏幕分辨率等信息来将用户请求转发到相应站点的实现。手机支持的协议、分辨率、型号等是根据Useragent查询wurfl得到的。本文只是基于实现的目的,如果要实际应用可能还需要很多改进。

参考:http://www.28lines.com/?p=11
Uwsgi配置参考:https://library.linode.com/web-servers/nginx/python-uwsgi/debian-5-lenny

一、Nginx处理流程图:





二、本案需要使用如下第三方模块重新编译nginx:,各模块可以到nginx官方网站下载。
1 Upstream Keepalive– Provides keep-alive connections to Memcached upstreams.
2 Lua– Embed the power of Lua into Nginx.
3 Set Hash– Set a variable to a variety of hash functions (upper/lowercase), including MD5.
4 uWSGI– Allows Nginx to interact with uWSGI processes.

lai@debian:~/ua-detect-nignx/nginx# ls -l debian/modules/
total 32
drwxr-xr-x 6 root root 4096 2011-05-03 14:04 chaoslawful-lua-nginx-module
drwxr-xr-x 9 root root 4096 2011-04-16 16:19 nginx-development-kit
drwxr-xr-x 6 root root 4096 2011-04-16 16:19 nginx-echo
drwxr-xr-x 4 root root 4096 2011-04-16 16:19 nginx-http-push
drwxr-xr-x 2 root root 4096 2011-04-16 16:19 nginx-upstream-fair
drwxr-xr-x 3 root root 4096 2011-04-30 13:52 ngx_http_upstream_keepalive
drwxr-xr-x 2 root root 4096 2011-04-30 13:52 simpl-ngx_http_set_hash
-rw-r--r-- 1 root root 696 2011-03-22 11:13 versions

Nginx编译选选:
lai@debian:~/ua-detect-nignx/nginx# vi debian/rules
111 config.status.extras: config.env.extras config.sub config.guess
112 cd $(BUILDDIR_extras) && ./configure /
113 --conf-path=/etc/nginx/nginx.conf /
114 --error-log-path=/var/log/nginx/error.log /
115 --http-client-body-temp-path=/var/lib/nginx/body /
116 --http-fastcgi-temp-path=/var/lib/nginx/fastcgi /
117 --http-log-path=/var/log/nginx/access.log /
118 --http-proxy-temp-path=/var/lib/nginx/proxy /
119 --http-scgi-temp-path=/var/lib/nginx/scgi /
120 --http-uwsgi-temp-path=/var/lib/nginx/uwsgi /
121 --lock-path=/var/lock/nginx.lock /
122 --pid-path=/var/run/nginx.pid /
123 --with-debug /
124 --with-http_addition_module /
125 --with-http_dav_module /
126 --with-http_flv_module /
127 --with-http_geoip_module /
128 --with-http_gzip_static_module /
129 --with-http_image_filter_module /
130 --with-http_perl_module /
131 --with-http_random_index_module /
132 --with-http_realip_module /
133 --with-http_secure_link_module /
134 --with-http_stub_status_module /
135 --with-http_ssl_module /
136 --with-http_sub_module /
137 --with-http_xslt_module /
138 --with-ipv6 /
139 --with-sha1=/usr/include/openssl /
140 --with-md5=/usr/include/openssl /
141 --add-module=$(MODULESDIR)/nginx-development-kit /
142 --add-module=$(MODULESDIR)/ngx_http_upstream_keepalive /
143 --add-module=$(MODULESDIR)/nginx-upstream-fair /
144 --add-module=$(MODULESDIR)/nginx-echo /
145 --add-module=$(MODULESDIR)/chaoslawful-lua-nginx-module /
146 --add-module=$(MODULESDIR)/simpl-ngx_http_set_hash /
147 $(CONFIGURE_OPTS) >$@
148 touch $@

在生成deb包之前也可以修改nginx的版本信息,避免因nginx本身漏洞导致站点被入侵。
Debian方式生成deb包:
lai@debian:~/ua-detect-nignx/nginx# dpkg-buildpackage -b
lai@debian:~/ua-detect-nignx# dpkg -i nginx-common_1.0.0-2_all.deb
lai@debian:~/ua-detect-nignx# dpkg -i nginx-extras_1.0.0-2_amd64.deb
lai@debian:~/ua-detect-nignx# dpkg -i nginx-extras-dbg_1.0.0-2_amd64.deb

查看安装的nginx软件包:
lai@debian:~/ua-detect-nignx# dpkg -l|grep nginx
ii nginx-common 1.0.0-2 small, but very powerful and efficient web server (common files)
ii nginx-extras 1.0.0-2 nginx web server with full set of core modules and extras
ii nginx-extras-dbg 1.0.0-2 Debugging symbols for nginx (extras)

查看nginx的编译选项:
lai@debian:~# nginx -V
nginx: nginx version: nginx/1.0.0
nginx: TLS SNI support enabled
nginx: configure arguments: --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module --with-http_dav_module --with-http_flv_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_perl_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --add-module=/home/lai/ua-detect-nignx/nginx/debian/modules/nginx-development-kit --add-module=/home/lai/ua-detect-nignx/nginx/debian/modules/ngx_http_upstream_keepalive --add-module=/home/lai/ua-detect-nignx/nginx/debian/modules/nginx-upstream-fair --add-module=/home/lai/ua-detect-nignx/nginx/debian/modules/nginx-echo --add-module=/home/lai/ua-detect-nignx/nginx/debian/modules/chaoslawful-lua-nginx-module --add-module=/home/lai/ua-detect-nignx/nginx/debian/modules/simpl-ngx_http_set_hash

Nginx配置:
lai@debian:~# more /etc/nginx/sites-available/default
# Mobile upstream.
upstream mobile {
server 127.0.0.1:81;
}

# Desktop upstream.
upstream desktop {
server 127.0.0.1:82;
}

# Memcached upstream used to store device upstream values.
upstream memcached {
# server ${memcached-supervisor:interface}:${memcached-supervisor:port};
server 127.0.0.1:11211;
keepalive 1024 single;
}

server {
listen 80;
server_name localhost;

# Lookup upstream in uWSGI.
location /map-request/dynamic/ {
include uwsgi_params;
# uwsgi_param MEMCACHED_SOCKET ${memcached-supervisor:interface}:${memcached-supervisor:port};
uwsgi_param MEMCACHED_SOCKET 127.0.0.1:11211;
uwsgi_pass 127.0.0.1:9001;
}

# Lookup upstream in Memcached.
location /map-request/cached/ {
set_md5 $memcached_key $http_user_agent;
memcached_pass memcached;
}

# Pass request to user-agent appropriate upstream.
location / {
# Lookup device upstream.
set $upstream "";
access_by_lua '
local result = ngx.location.capture("/map-request/cached/")
if result.status == 200 then
ngx.var.upstream = result.body
else
local result = ngx.location.capture("/map-request/dynamic/")
ngx.var.upstream = result.body
end
ngx.exit(ngx.OK)
';
proxy_pass http://$upstream; proxy_set_header Host $http_host;
}
}

lai@debian:~# more /etc/nginx/sites-available/mobile
server {
listen 81;
server_name localhost;

location / {
default_type text/plain;
echo "mobile instance";
}
}

lai@debian:~# more /etc/nginx/sites-available/desktop
server {
listen 82;
server_name localhost;

location / {
default_type text/plain;
echo "desktop instance";
}
}

三、下载nginx手机适配整个软件包praekelt-nginx-ua-instance-switcher-c69f54d.tar.gz,在部署时使用:
https://github.com/praekelt/nginx-ua-instance-switcher

四、下载安装uwsgi及python的ua-mapper库
下载uwsgi:
lai@debian:~/ua-detect-nignx#wget http://projects.unbit.it/downloads/uwsgi-0.9.7.2.tar.gz
lai@debian:~/ua-detect-nignx# tar xzvf uwsgi-0.9.7.2.tar.gz
lai@debian:~/ua-detect-nignx# cd uwsgi-0.9.7.2

安装uwsgi:
lai@debian:~/ua-detect-nignx/uwsgi-0.9.7.2#apt-get install build-essential psmisc python-dev libxml2 libxml2-dev python-setuptools
lai@debian:~/ua-detect-nignx/uwsgi-0.9.7.2# make -f Makefile.Py26
lai@debian:~/ua-detect-nignx/uwsgi-0.9.7.2# cp uwsgi /usr/bin/
或者lai@debian:~/ua-detect-nignx/uwsgi-0.9.7.2# python setup.py install

添加uwsgi用户
lai@debian:~/ua-detect-nignx/uwsgi-0.9.7.2#adduser --system --no-create-home --disabled-login --disabled-password --group uwsgi

下载安装python的ua-mapper库
下载地址:https://github.com/praekelt/wsgi-ua-mapper
lai@debian:~/ua-detect-nignx# cd wsgi-ua-mapper-0.0.3
lai@debian:~/ua-detect-nignx/wsgi-ua-mapper-0.0.3# python setup.py install

Uwsgi配置:
lai@debian:/data# more /etc/default/uwsgi
PYTHONPATH=/data/webroot/uamapper/mapper
MODULE=wsgi


Uwsgi启动脚本:
lai@debian:/data# more /etc/init.d/uwsgi
#!/bin/sh
### BEGIN INIT INFO
# Provides: uwsgi
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the uwsgi app server
# Description: starts uwsgi app server using start-stop-daemon
### END INIT INFO

PATH=/opt/uwsgi:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/uwsgi

OWNER=uwsgi

NAME=uwsgi
DESC=uwsgi

test -x $DAEMON || exit 0

# Include uwsgi defaults if available
if [ -f /etc/default/uwsgi ] ; then
. /etc/default/uwsgi
fi

set -e

DAEMON_OPTS="-s 127.0.0.1:9001 -M 4 -t 30 -A 4 -p 4 -d /var/log/uwsgi.log --pythonpath $PYTHONPATH --module $M
ODULE"

case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --chuid $OWNER:$OWNER --user $OWNER /
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --signal 3 --user $OWNER --quiet --retry 2 --stop /
--exec $DAEMON
echo "$NAME."
;;
reload)
killall -1 $DAEMON
;;
force-reload)
killall -15 $DAEMON
;;
restart)
echo -n "Restarting $DESC: "
start-stop-daemon --signal 3 --user $OWNER --quiet --retry 2 --stop /
--exec $DAEMON
sleep 1
start-stop-daemon --user $OWNER --start --quiet --chuid $OWNER:$OWNER /
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
status)
killall -10 $DAEMON
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
exit 1
;;
esac
exit 0


将uwsgi加入到开机启动
lai@debian:~/ua-detect-nignx/uwsgi-0.9.7.2#/usr/sbin/update-rc.d -f uwsgi defaults
lai@debian:~/ua-detect-nignx/uwsgi-0.9.7.2#/etc/init.d/uwsgi start

五、分别用手机和pc访问,查看是否转向不同站点:
测试:http://ip
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: