配置:
https://www.nginx.com/resources/wiki/start/topics/examples/full/#

ng config 使用if出错导致无法正常启动

1
2

if($domain = "test") {}

上面代码会报错 nginx unknown directive “if( ,导致无法启动

1
if ($domain = "test") {}

解决方法: 在 if 后面添加一个空格,另外注意,如果是字符串需要 双引号包起来。
*if 不赞成使用,具体请参考:https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/#why-this-happens-and-still-not-fixed

nginx 配置静态资源cdn 出现404


问题代码如下:

1
2
3
location ^~/cdn/ {
root cdn;
}

由于 url 为 http:ip:port/cdn/xxx/index.js
但是rott 配置为 cdn目录, $uri = /cdn/xxx/index.js
nginx 在cdn目录下无法找到 $uri,所以一直报404

正确配置如下:

1
2
3
4
location ^~/cdn/ {
# 将cdn目录移至 test目录内
root test;
}

nginx配置代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
http {
upstream mobile_dev_server {
server 127.0.0.1:8085;
keepalive 2000;
}
server {
listen 8282;
server_name localhost;

location ^~/mobile {
proxy_pass http://mobile_dev_server/mobile;
proxy_set_header Host $host:$server_port;
}
}
}

127.0.0.1:8282/mobile 跳转至 127.0.0.1:8085/mobile


nginx挂载目录浏览

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 4445;
server_name localhost;

#charset koi8-r;
charset utf-8;
location / {
root D:/Levana.Xue/Mobile-App-APK-Code-Backup; #指定实际目录绝对路径
autoindex on; #开启目录浏览功能
autoindex_exact_size off; #关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b
autoindex_localtime on; #开启以服务器本地时区显示文件修改日期!
}
}

window10下nginx操作命令

http://nginx.org/en/docs/windows.html

1
2
3
4
5
6
7
# 执行以下命令需在nginx目录下(与nginx.exe 同级) 执行

$nginx -s reload #重载nginx cnfig

$start nginx #启动nginx

$nginx -s stop 停止运行nginx