关于nginx 配置二级域名问题

server {
        listen 8088;

        server_name test.example.com;

        root /home/www/test_tec.example.com/upload;
        index index.html index.php;

        access_log log/test_tec_access.log;
        error_log log/test_tec_error.log;

        location / {
                include /home/www/test_tec.example.com/upload/*.conf;
                index index.html index.php;
        }

        location /wiki {
                alias /home/www/wiki.example.com/;
                index index.html index.php;
        }

        location ~ \.php {
                include ext/php.conf;
                include ext/cors.conf;
        }

        include ext/expired.conf;
}

请教一下nginx配置二级域名问题
之前location /wiki 配置规则用的是 root 通过url访问,总会在root后多加一句/wiki

看了一些教程,说用alias 可以避免这个问题,于是改成图中这样的规则,但是现在访问该二级直接报错
File not found.

error.log

2018/02/02 09:24:47 [error] 13644#0: *20723710 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 49.77.232.248, server: test.example.com, request: "GET /wiki/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "test.example.com:8088"

请问如何解决??

阅读 3.3k
2 个回答

如果某个location里要配置alias指令
请在路径后面加上 /
比如

location /example/ {
    alias /path/to/exam/;
}

另外配置请贴文字,要打马赛克用example替换。


基于楼主补充的日志
问题出在 location /wiki/
这个location使用了alias,但后面还有一个location ~ \.php,这样先匹配wiki再匹配\.php,到\.php,环境还是server块的root指令

解决办法:
如果一个location里使用了alias,但又会匹配到其他location,需要把其他location的配置加到这个location里

location /wiki {
    alias /home/www/wiki.example.com/;
    index index.html index.php;
    location ~ \.php$ {
        include ext/php.conf;
        include ext/cors.conf;
        include fastcgi_params;                       
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

把你的error.log贴出来看看,应该会告诉你是哪个文件file not found了,还有路径,这样你就很容易找到问题在哪。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题