nginx的安装&一个最简单的配置(windows和Centos)

Windows 11:

1、下载nginx(版本为nginx-1.22.1)

https://nginx.org/en/download.html
nginx的目录最好不要有中文,不建议放到C盘。

2、最基础的配置nginx.conf如下:

  配置使用前后端分离。前端静态代码放到../../zym/html中,访问端口是80。后端接口的链接地址是8081端口。

  例如访问 http://localhost/data/index.html 实质是访问../../zym/html/data/index.html文件

  访问htpp://localhost/api/user/user_info 实质是访问http://localhost:8081/user/user_info接口,注意url中的api会被删掉。对于proxy中url中是否有斜杠,比较复杂,具体规则参看:https://www.jianshu.com/p/fc91f00016e4 。本文附录1也有介绍,完全copy自该链接。

nginx.conf

...
server {
    listen       80;
    server_name  localhost;

    location  /api/ {                         #规定好了,接口都是api开头的。
        proxy_pass   http://localhost:8081/;  #对于api的请求,通过nginx转发到8081端口
    }

    location / {
        root   ../../zym/html;       #放置静态代码的地方
        index  index.html index.htm; #默认主页
    }
...
}
...

3、运行

启动nginx : start nginx
终止nginx: nginx -s stop
更新conf后需要刷新nginx(这个最常用,一般不会随便终止nginx):nginx -s reload

在Linux下安装(Centos 7)

其对于conf文件的配置与windows相同。只是路径根据实际情况修改一下即可。

1、去下载以下网址下载,然后放到linux去

http://nginx.org/download/nginx-1.22.1.tar.gz
或者直接用命令wget http://nginx.org/download/nginx-1.22.1.tar.gz

2、安装过程(参考https://www.cnblogs.com/Zzzyyw/p/17020893.html):

(1)新建目录并解压
cd /usr/local/
mkdir nginx
cd nginx/ # 上传文件(或者用wget下载)
ls # nginx-1.22.1.tar.gz
tar -zxvf nginx-1.22.1.tar.gz
(2)安装
ls
cd nginx-1.22.1
./configure --prefix=/usr/local/nginx # ./configure --prefix=路径
make
make install
# 两命令可简写为make && make install

(3)启动
进入安装好的目录 /usr/local/nginx/sbin

./nginx # 启动
./nginx -s stop # 快速停止
./nginx -s quit # 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload # 重新加载配置

  

附录1【Proxy,斜杠规则】:

结论,简单理解为:代理的地址在端口以后如果有东西(包括目录或者/),转发地址会去除location匹配的目录(根据匹配的字符,如果是/api则去除api,如果是/api/则去除/api/)
如果代理地址到端口就没了(没有目录或/),那么转发地址会保留匹配目录

(1)location和proxy_pass都带/,则真实地址不带location匹配目录
location /api/{proxy_pass http://127.0.0.1:8080/;}

访问地址:www.test.com/api/upload-->http://127.0.0.1:8080/upload

2.location不带/,proxy_pass带/,则真实地址会带/
location /api{proxy_pass http://127.0.0.1:8080/;}

访问地址: www.test.com/api/upload-->http://127.0.0.1:8080//upload

3.location带/,proxy_pass不带/,则真实地址会带location匹配目录/api/
location /api/{proxy_pass http://127.0.0.1:8080;}

访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/api/upload

4.location和proxy_pass都不带/,则真实地址会带location匹配目录/api/
location /api{proxy_pass http://127.0.0.1:8080;}

访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/api/upload

5.同1,但 proxy_pass带地址
location /api/{proxy_pass http://127.0.0.1:8080/server/;}

访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/server/upload

6.同2,但 proxy_pass带地址,则真实地址会多个/
location /api{proxy_pass http://127.0.0.1:8080/server/;}

访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/server//upload

7.同3,但 proxy_pass带地址,则真实地址会直接连起来
location /api/{proxy_pass http://127.0.0.1:8080/server;}

访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/serverupload

8.同4,但 proxy_pass带地址,则真实地址匹配地址会替换location匹配目录
location /api{proxy_pass http://127.0.0.1:8080/server;}

访问地址: www.test.com/api/upload-->http://127.0.0.1:8080/server/upload

总结

1.proxy_pass代理地址端口后有目录(包括 / ),转发后地址:代理地址+访问URL目录部分去除location匹配目录
2.proxy_pass代理地址端口后无任何,转发后地址:代理地址+访问URL目录部

posted @ 2019-08-13 17:17  东方春  阅读(409)  评论(0编辑  收藏  举报