`
wdhdmx
  • 浏览: 300163 次
  • 性别: Icon_minigender_1
  • 来自: 山西
博客专栏
D4bbb5f7-9aa4-3e66-8194-f61b3f0241c2
天天编程
浏览量:21494
社区版块
存档分类
最新评论

nginx解决www和wap的选择访问

阅读更多

解决手机端访问www网站时切换到wap网页,同时pc机访问wap时切换到www端。

1.在windows下第一次使用nginx。实现服务器分担访问。

  • 在 http://wiki.nginx.org/NginxChsInstall 下载nginx0.8.54 windows版本。
  • 安装到能找到的地方就行,例如:C:\nginx-0.8.54。
  • 配置conf文件下的nginx.conf。这个文件时nginx的配置文件。
  • nginx.conf文件中找到server{},在它的上面加入 : 
     
    upstream wan
     {
    server www.baidu.com:80;
    server www.google.cn:80;
    }
       这里是均衡服务器,客户请求跳转到这个目录,然后依次(还是随机?)的将请求转送到各个服务器。
  •  然后把server{} 中的location 改为:
    location / { 
    proxy_pass http://wan;
    proxy_redirect default;
    }
        这里配置的跳转到上面的均衡服务器,这个localhost中还可以加入逻辑判断等功能。
  •  全部配置文件
    #注释符号#。
    #工作进程数
    worker_processes  1;
    events {
    	#允许连接数。
    	worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
    #均衡服务器,负责分担访问的。
    upstream wan
     {
    server www.baidu.com:80;
    server www.google.cn:80;
    }
     server {
    	#监听的端口
            listen       80;
    	#这里可以填自己机器的ip
            server_name  localhost;
            location / {
    		proxy_pass http://wan;
    		proxy_redirect default;
            }
        }
    }
     
  • 运行start nginx 、 退出    nginx –s quit 重载 Nginx -s reload。运行start nginx。
  • 多次打开页面 http://localhost。就会发现访问的分别是这两个网站。

 

2.限制火狐和IE浏览器访问的配置文件。

server {
	#监听的端口
        listen       80;
	#这里可以填自己机器的ip
        server_name  localhost;
        location / {
	#限制UA为ie和火狐的浏览器
	if ($http_user_agent ~ (MSIE)|(Mozilla) ){
	#503 是服务器出错的一种返回状态
	return 503;
	}
		proxy_pass http://wan;
		proxy_redirect default;
        }
    }

 这样火狐或者ie访问localhost的时候就会出现503的访问错误。

 

 

3.www和wap的访问切换。

猫扑采用的就是nginx。它实现了wap和www的切换,除了pc的网页浏览器,似乎任意的User-Agent(例如:ttpod,kkx,(abc不行))都可以访问它们的wap站点,所以估计通过猫扑是通过user-agent来判断是否为pc的浏览器,然后进行跳转。目前有现成的手机端浏览器的UA,这里先做了为手机端的一些判断,如果是手机直接跳转到wap,如果不是跳到www。

worker_processes  1;
events {
    #允许连接数
    worker_connections  50000;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  off;
    upstream wwwxxx{
        server www.xxx.com;
    }
    upstream wapxxx{
	server wap.xxx.com;
        #server 117.135.xxx.xx:9090;
    }
    server {
      listen       80;
        server_name  localhost;
        location / {
	#set $ismob 0;这里可以设置一个变量.
       #这里大小写敏感,~*无视大小写。
	if ($http_user_agent ~ "((MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC\-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT)|(SonyEricsson)|(NEC)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC)|(SED)|(EMOL)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera))"  )
	{
		#set $ismob 1;
		proxy_pass  http://wapxxx;
	}
        #空的UA默认为手机访问。
	if ( $http_user_agent ~ ^$ )
	{
		#set $ismob 1;
		proxy_pass http://wapxxx; 
	}		
        proxy_pass http://wwwxxx;
	proxy_redirect default;
	}
    }
}

这里推荐使用filefox中的user agent switcher 来模拟各个不通的UA。

这基本就能做到www和wap的选择。不过,这个每次都要进行判断的话,严重影响速度。所以有谁知道该怎么做www和wap跳转的方法就告诉我,谢谢了。

我是新手,望各位大侠指正。

 

 

分享到:
评论
1 楼 rongton 2012-01-14  
user  www www;

worker_processes 4;

error_log  logs/nginx_error.log  crit;

pid        /usr/local/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
{
use epoll;
worker_connections 51200;
}

http
{

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

charset utf-8;
    
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
    
sendfile on;
tcp_nopush     on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

gzip on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;

#limit_zone  crawler  $binary_remote_addr  10m;


upstream wwwtodayinns{  
   server www.todayinns.com;  
   index index.php index.html index.htm;
   root  /var/www/html;
   error_page 404 = /notfound.html;
  
    location  /{
    

rewrite "^/sns$" /sns.php last;
rewrite "^/sns/create$" /sns.php?do=create last;
rewrite "^/sns/([1-9][0-9]*)$" /sns.php?_id=$1 last;
rewrite "^/news$" /news.php last;
rewrite "^/news/([1-9][0-9]*)$" /news.php?_id=$1 last;
rewrite "^/news/([a-zA-Z][0-9a-zA-Z]*)$" /news.php last;
rewrite "^/book$" /book.php last;
rewrite "^/book/([a-zA-Z]+)$" /book.php?_mod=$1 last;
rewrite "^/comment/([1-9][0-9]*)$" /comment.php?_id=$1 last;
rewrite "^/branch$" /branch.php last;
rewrite "^/branch/([1-9][0-9]*)$" /branch.php?_id=$1 last;
rewrite "^/admin$" /admin/admin.php last;
rewrite "^/admin/([a-z]+)$" /admin/admin.php?_mod=$1 last;
rewrite "^/admin/([a-z]+)/([^/]+)$" /admin/admin.php?_mod=$1&_id=$2 last;
rewrite "^/admin/([a-z]+)/([a-z]+)/([^/]+)$" /admin/admin.php?_mod=$1&_id=$2&_type=$3 last;       

          } 
  
 
        
    location ~ .*\.(php|php5)?$
   {     
     #fastcgi_pass  unix:/tmp/php-cgi.sock;
     fastcgi_pass  127.0.0.1:9000;
     fastcgi_index index.php;
     include fcgi.conf;

   }
  
   
   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
   {
     expires      30d;
   }
   
   location ~ .*\.(js|css)?$
   {
     expires      1h;
   }   
   
   log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
   access_log  logs/access.log  access;
   }  
    upstream waptodayinns{  
    server wap.todayinns.com;  
       server_name  www.todayinns.com;
   index index.php index.html index.htm;
   root  /var/www/html;  
 
  
     }


   
       
      
       
}

相关推荐

Global site tag (gtag.js) - Google Analytics