上手
下载
http://nginx.org/en/download.html
而后解压到某个门路下
启动
cmd进入nginx目录下
例如我是解压到E盘,那么我的nginx的路劲是
E:\nginx-1.19.0
window 下启动nginx
start nginx
或者双击nginx.exe
拜访localhost:80
如果无法访问,查看80端口是否被占用
敞开nginx
nginx目录下
./nginx.exe -s stop
罕用api
root
默认依据路是html文件夹下
拜访locahost/a/index.html时,查找的就是html目录下a/index.html
location /a { root html; index index.html index.htm;~~~~ }
将根目录批改为html/helpcenter
拜访localhost/a/index.html时,查找的就是html/helpcenter/a/index.html
location /a { root html/helpcenter/; index index.html index.htm; }
alias
同样的配置,拜访localhost/a/index.html时,寻找的是html/helpcenter/index.html
location /a { alias html/helpcenter/; index index.html index.htm; }
listen
监听多个端口,对应同样的逻辑
server { listen 8000; listen 8080; location / { root html; index index.html index.htm;}
监听多个端口,对应不同的逻辑
server { listen 8000; location / { root html; index index.html index.htm;}server { listen 8001; location / { root html/a/; index index.html index.htm;}
try_files
语法:
try_files file ... uri;try_files file ...=code;
例子:
location / { try_files $uri /index.html}
拜访 http://localhost/b
如果不存在b
的目录,那么会报404
,在设置了try_files ,nginx 会先查找b/index.html
,发现找不到,那就返回默认的/index.html
uri: 代表的是默认门路
nginx module
ngx_http_index_module
定义作为首选项的文件,file
名称能够蕴含变量。依照配置的程序一个个查
语法:
index file ...;
例子:
location / { index index.html index.htm;}
如果没有配置,默认是index.html