【051】利用nginx的官方Docker镜像建立自动区分手机和PC的静态资源服务器

本文是在Linux ubuntu14.04操作系统上做的配置。
所有的文件都是UTF-8编码,换行符使用了Linux的形式。即只有换行符,而区别于Windows使用的回车换行符。
首先利用mkdir命令,建立下面几个目录:
/zhangchao/nginx/config 存放nginx配置文件的目录
/zhangchao/nginx/projects/pc 存放网站PC版的前端文件
/zhangchao/nginx/projects/mobile 存放网站手机版的前端文件

在 /zhangchao/nginx/config 下新建一个文件,命名为nginx.conf。
下面是nginx.conf的内容:


user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
worker_rlimit_nofile 100000;


events {
    worker_connections  2048;
    multi_accept on;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        on;
    #tcp_nopush     on;
    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server_tokens off;
    tcp_nopush on;
    tcp_nodelay on;
    access_log off;
    error_log /var/log/nginx/error.log crit;
    keepalive_timeout 10;
    client_header_timeout 10;
    client_body_timeout 10;
    reset_timedout_connection on;
    send_timeout 10;
    limit_conn_zone $binary_remote_addr zone=addr:5m;
    limit_conn addr 100;
    charset UTF-8;
    gzip on;
    gzip_disable "msie6";
    gzip_proxied any;
    gzip_min_length 1000;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    open_file_cache max=100000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
    include /etc/nginx/sites-enabled/*;

    server {
        listen 80;
        server_name 192.42.41.54;
        error_page  404              http://121.42.41.54/404.html;
        access_log  /zhanchao/nginx/projects/pc/access.log;
        location / {
            root /zhanchao/nginx/projects/pc;
            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-)|(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)" )
            {
                root /zhanchao/nginx/projects/mobile;
            }
            index index.html;
        }
    }
}


PC版和手机版的目录里面都要放入一个404.html文件,用来显示404错误提示信息。

PC版和手机版的目录里面都要保证存在一个index.html文件作为入口文件。由于PC和手机屏幕的差异,前端需要分别设计两个不同页面,所以放在两个不同的目录里面。下面是本例子中的index.html内容,因为仅仅是作为演示,所以只有提示是PC和手机的字符。

如果用户只是想要使用IP来访问网站的话,把Server对象里面的 server_name 设置成Docker宿主机的IP。这里假设IP是192.42.41.54。

PC index.html

<html>
    <head>
        <title>aaaaaaa</title>
    </head>
    <body>
        PC电脑
    </body>
</html>

手机 index.html

<html>
    <head>
        <title>aaaaaaa</title>
    </head>
    <body>
        mobile
    </body>
</html>

最后用nginx官方镜像建立容器:

docker run --name nginx-a \
-v /zhanchao/nginx/config/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /zhanchao/nginx/projects/pc:/zhanchao/nginx/projects/pc \
-v /zhanchao/nginx/projects/mobile:/zhanchao/nginx/projects/mobile \
-p 80:80 -d nginx:latest
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值