首页 > 解决方案 > Nginx 动态虚拟主机返回 ERR_INVALID_REDIRECT

问题描述

我正在尝试将服务器设置为使用单个 conf 文件来为所有开发站点提供服务。所以我的 /ect/nginx/site-available/dev.example.com 看起来像这样:

server {
    listen 80;

    server_name ~^(?<folder>[^.]*).(?<user>[^.]*).dev.example.com;

    charset utf-8;
    index index.php index.html index.htm;



    root /var/www/projects/dev/$user/$folder/htdocs;
    access_log /var/www/projects/dev/$user/$folder/access.log;
    error_log  /var/www/projects/dev/commons/logs/htdocs/error.log;

    location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|js|css)$ {
        access_log        off;
        log_not_found     off;
        expires           30d;
        add_header Pragma "public";
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }

    location ~ \.php$ {

            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param APPLICATION_ENV $user;
            include fastcgi_params;
   }


    location ~ /\.ht {
        deny all;
    }
}

正则表达式工作正常,如果我使用带有 phpinfo() 的 index.php 运行http://energy.magana.dev.emaple.com,则文件正在工作,似乎一切正常......但如果我运行 wordpress我得到 ERR_INVALID_REDIRECT。

如果我在没有正则表达式的情况下更改文件 /ect/nginx/site-available/dev.example.com 并设置绝对路径,一切正常。

我缺少什么?

标签: regexnginxvhosts

解决方案


问题是变量 SERVER_NAME 被解释为~^(?<folder>[^.]*).(?<user>[^.]*).dev.example.com修复这个覆盖变量的工作

fastcgi_param SERVER_NAME $folder.$user.dev.example.com;

推荐阅读