美文网首页
Nginx快速上手

Nginx快速上手

作者: 我叫Aliya但是被占用了 | 来源:发表于2019-04-20 13:43 被阅读0次

参考 https://mp.weixin.qq.com/s/BA_JZ_kMBFZBE7jjQDNc1Qhttps://www.cnblogs.com/meng1314-shuai/p/8335140.htmlhttps://www.cnblogs.com/hellowoeld/p/8279700.html

一直用iis,后来用apache,再之后被告知nginx是主流,现在来用最短时间,用nginx架一个站点吧

下载安装(windows)

官方下载,选择Stable version(稳定版)zip包。
解压后不用安装,双击解压后的nginx.exe启动服务

下载安装(mac)

> brew update  // 检测brew命令是否可用
-bash: brew: command not found

> ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 
一坨 输入密码 一坨

> brew search nginx   //查询要安装的软件是否存在
==> Formulae
nginx

> brew info nginx
nginx: stable 1.15.12 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
Not installed  // 没安装
...

> brew install nginx     // 安装
   To have launchd start nginx now and restart at login:
         brew services start nginx
   Or, if you don't want/need a background service you can just run:
         nginx
> nginx // 启动服务

> open /usr/local/etc/nginx/   // 打开nginx文件夹,配置文件就在此处

常用命令

nginx -s reload  :修改配置后重新加载生效
nginx -s reopen  :重新打开日志文件
nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确
nginx -s stop  :快速停止nginx
         quit  :完整有序的停止nginx
kill -QUIT 主进程号     :从容停止Nginx
kill -TERM 主进程号     :快速停止Nginx
pkill -9 nginx          :强制停止Nginx
启动nginx:
nginx -c /path/to/nginx.conf

平滑重启nginx:
kill -HUP 主进程号
nginx -V   // 安装位置
nginx -t   // 配置文件位置
vim [文件地址]  // 打开文件
i // 编辑
按Esc键回到命令模式,:q ! (:wq) (q 退出程序 w 保存文件)

unzip xx.zip -d 解压到指定目录

配置文件

在安装目录查找nginx.conf文件,此为配置文件,下面开始架站点

// nginx.conf
http {
    // http://location:8000/
    server {
        listen       8000;
        location / {
            root   D:/root/website;    // 即使在win下也是这种格式,不是D:\\root\\...
            index  index.html index.htm;
            try_files $uri $uri/ /index.html; // 解决vue页面刷新404问题
        }
    }
    // 域名重定向 http://location:8000/ => 80端口
    server {
        listen       80;
        // server_name  www.hahaha.com;
        location / {
            proxy_pass   http://location:8000;
        }
    }

    server {
        // 根据状态码过滤
        error_page 500 501 502 503 504 506 /error.html;
        location = /error.html {
            root D:/root/website/error.html;
        }
        // 根据URL名称过滤,精准匹配URL,不匹配的URL全部重定向到主页
        location / {
            rewrite  ^.*$ /index.html  redirect;
        }
    }

    server {
        listen       8081;
        location / {
            ## 解决跨域
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
            if ($request_method = 'OPTIONS') {
                return 204;
            }

            root   D:/root/api;    
            index  index.html index.htm;
        }
    } 
}

gzip

gzip浏览器支持情况(2019.4)

嗯,支持的情况不是特别好

http{
    gzip                    on;
    gzip_http_version       1.1;
    gzip_comp_level         5;
    gzip_min_length         1000;
    gzip_types text/csv text/xml text/css text/plain text/javascript application/javascript application/x-javascript application/json application/xml;
}

静态资源服务器

location ~* \.(png|gif|jpg|jpeg)$ {
    root    /root/static/;
    autoindex on;    // 开启浏览目录权限
    access_log  off;
    expires     10h;  // 设置缓存过期时间为  10小时
}

负载均衡

通过配置就可以实现负载均衡

反向代理

使用proxy_pass就可以实现简单的反向代理

相关文章

网友评论

      本文标题:Nginx快速上手

      本文链接:https://www.haomeiwen.com/subject/djycgqtx.html