通过 Nginx 实现在手机上播放电脑中的视频

这两天在学校看剧,由于是 BT 资源,就把整个剧集下载了,但是坐着看看累了,想躺着看。立即想到在电脑上搭建个 Web 服务器,然后开热点,用手机连接热点,打开视频播放。 涉及到:
  • Nginx 的安装与基本配置
  • 符号链接
  • Nginx 打开目录索引(支持中文名)
  • Nginx 403 Forbidden 权限问题
  • 本地可以访问,其他机器不能访问
当然,现在想起来,其实也可以使用 Python 自带那个 HttpServer 实现的;不过之前装好了 Nginx 所以一下先想到的是 NG. 首先是安装 Nginx,Linux 可以使用包管理器直接安装,我觉得没必要从源码自己 make 安装,太麻烦了;Mac 可以使用 HomeBrew 安装,相当于一个包管理器。 Mac 下安装好后,服务器根目录在 /usr/local/var/www/ 配置目录在 /usr/local/etc/nginx/, 为了方便存取根目录文件,可以建一个符号链接到 Home 目录下,就是相当于一个快捷方式。
1
ln -s /usr/local/var/www ~/www 
ln 命令用于创建符号链接,由于链接的是目录,只能创建软链接,用 -s 指定,然后是源目录,跟着新建的目录。见:http://man.linuxde.net/ln 然后可以把要供访问的文件放在 www 目录下就行了,这样就可以通过 HTTP 访问到。我这里的视频都在 ~/Download/video 中,于是又在 www 下新建了个符号链接,链接到 video 目录。 为了打开网址可以看到文件列表,需要配置 Nginx 列出文件,可以在 location 段中加入
1
2
3
4
5

    autoindex on;  #自动显示目录
    autoindex_exact_size off; #人性化方式显示文件大小否则以byte显示
    autoindex_localtime on; #按服务器时间显示,否则以gmt时间显示
    charset utf-8,gbk; #中文乱码
重启 Nginx 发现出现 403,经过搜索,怀疑是文件权限或用户组的问题,然后列了下可以正常访问的文件和 403 的文件,果然是用户组不一样。 在我这,可以正常访问的文件的用户组是 admin,403 的用户组是 staff。而 Nginx 现在是主进程是 root 用户,子进程是我的用户名,因此在 nginx.conf 第一行加上 user xxx admin; 就好了(xxx 是我用户名) 最后一个奇怪的问题,在本地,用 域名或 IP 地址都可以访问到,但是链接到我的热点的设备访问不了。 原来是监听端口的问题。 需要把监听该为 0.0.0.0 而不是 127.0.0.1 大概意思是 127.0.0.1 是本地环回地址,0.0.0.0 才是监听所有地址。但是貌似 80 端口还是不好使,换成 8080 就好了。具体原因还不清楚,不过我的需要倒是实现了哈哈哈哈 这是我的配置文件: cat /usr/local/etc/nginx/nginx.conf
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

user youthlin.chen admin;

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen     80;
        listen [::]:80;
        server_name  localhost;

        #access_log  logs/host.access.log  main;

        location / {
    autoindex on;  #自动显示目录
    autoindex_exact_size off; #人性化方式显示文件大小否则以byte显示
    autoindex_localtime on; #按服务器时间显示,否则以gmt时间显示
    charset utf-8,gbk; #中文乱码
            root   html;
            index index.php  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/var/www/$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}
cat /usr/local/etc/nginx/servers/test.conf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
upstream local_test{
    server 127.0.0.1:80;
    # server 127.0.0.1:8081;
}

server {
    listen 8080;
    listen [::]:8080;

    location / {
    autoindex on;  #自动显示目录
    autoindex_exact_size off; #人性化方式显示文件大小否则以byte显示
    autoindex_localtime on; #按服务器时间显示,否则以gmt时间显示
    charset utf-8,gbk; #中文乱码
        #真实IP,Java里就可以使用【request.getHeader("X-Real-IP")】获取IP
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        #上游真实地址
        proxy_pass http://local_test;
        try_files $uri $uri/ =404;
    }
}

Reader Echoes

4 comments

  • #1455zhujiwiki
    我一般开共享
  • #1454小菜
    好高端,我只会用电脑局域网共享文件来看,生命不息折腾不止
  • #1453黑暗游侠
    说实话,这样真的好麻烦,放到vps上就行了
  • #1452c
    果然技术大牛,竟然自己搭内网…… 我一般觉得复制到手机更舒服一些……

表情

评论提交后需经审核才会显示。