分类
电脑软件

通过 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 目录下,就是相当于一个快捷方式。

ln -s /usr/local/var/www ~/www 

ln 命令用于创建符号链接,由于链接的是目录,只能创建软链接,用 -s 指定,然后是源目录,跟着新建的目录。见:http://man.linuxde.net/ln

然后可以把要供访问的文件放在 www 目录下就行了,这样就可以通过 HTTP 访问到。我这里的视频都在 ~/Download/video 中,于是又在 www 下新建了个符号链接,链接到 video 目录。
为了打开网址可以看到文件列表,需要配置 Nginx 列出文件,可以在 location 段中加入

    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

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

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;
    }
}

“通过 Nginx 实现在手机上播放电脑中的视频”上的4条回复

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

[/鼓掌] [/难过] [/调皮] [/白眼] [/疑问] [/流泪] [/流汗] [/撇嘴] [/抠鼻] [/惊讶] [/微笑] [/得意] [/大兵] [/坏笑] [/呲牙] [/吓到] [/可爱] [/发怒] [/发呆] [/偷笑] [/亲亲]