nginx配置遇到的问题与解决方法(nginx.conf正则)

在:http://onlyzq.blog.51cto.com/1228/535279 中提到了使用正则表达式对server_name进行配置

但我配置下来,在使用location参数以后会导致:

访问location定义过的文件报404错误,但是能够访问未定义的文件,

经过排查,发现是以下代码问题:

server_name ~^([^.]+)\.([0-9]\.)?(baidu|sina)\.com$;
root  /home/wwwroot/$1/;
access_log  /home/wwwlogs/access_$1.log;

在nginx启动的时候,会被重复赋值,因此可以说,后一个配置会吧前一个配置覆盖.

因此将以上代码修改为

server_name ~^([^.]+)\.([0-9]\.)?(baidu|sina)\.com$;
set $www_root $1;
root  /home/wwwroot/$www_root/;
access_log  /home/wwwlogs/access_$www_root.log;


防止重复赋值导致出错

你可能感兴趣的:(代码生涯)