计算机本地静态资源用nginx发布

  • 下载软件

    下载地址:http://nginx.org/en/download.html 下载稳定版本,以http://nginx.org/download/nginx-1.16.0.zip为例,直接下载 nginx/Windows-1.16.0后解压,解压后文件目录结构如下:
    在这里插入图片描述

  • 启动nginx

    有很多种方法启动nginx
    1.直接双击nginx.exe,双击后一个黑色的弹窗一闪而过
    2.打开cmd命令窗口,切换到nginx解压目录下,输入命令 nginx.exe 或者 start nginx ,回车即可

  • 查看是否启动成功

    直接在浏览器地址栏输入网址 http://localhost:80,回车,出现以下页面说明启动成功
    在这里插入图片描述
    注意:如果没有出现这个页面,则启动失败,有可能是80端口被占用所致,我们可以尝试修改下nginx.conf文件的服务器端口号。

    nginx的配置文件是conf目录下的nginx.conf,默认配置的nginx监听的端口为80,如果80端口被占用可以修改为未被占用的端口即可

 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   E:/nginx-1.16.0/static;
            index  index.html index.htm;
        }

       ...
    }
  • 访问本地静态资源
    以我自己为例,将静态资源(如jpg|png|css|js等)放在如下配置的 E:/nginx-1.16.0/static;目录下,然后在nginx配置文件中做如下配置(注意:静态资源配置只能放在 location / 中),浏览器中访问 http://localhost:80/nginx.png 即可访问到 E:/nginx-1.16.0/static;目录下的 nginx.jpg图片
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   E:/nginx-1.16.0/static;
            index  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  /scripts$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;
        #}
    }

实现效果如下图所示:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值