【问题标题】:nginx cakephp virtual host redirectsnginx cakephp 虚拟主机重定向
【发布时间】:2026-01-23 19:50:01
【问题描述】:

我是 nginx 新手,无法让我的网站正常工作(AWS 上的 Cakephp 网站)。我收到消息“页面未正确重定向”以及某些内容如何将域附加到自身(重定向循环);

例如当我进入我的网站sub1.mysite.com 时,浏览器会添加sub1.mysite.com/sub1.mysite.com/sub1.mysite.com/sub1.mysite.com 等等。

这是我的站点可用配置

server {
   listen 80;
   server_name sub1.mysite.com;
   rewrite ^(.*) http://sub1.mysite.com$1 permanent;
}

server {
  listen   80; ## listen for ipv4; this line is default and implied
  server_name name sub1.mysite.com;
  root /var/www/sub1.mysite.com/public_html/sub-root;
  index  index.php index.html index.htm;

  # error_page 404 errors/404.html;
  access_log /var/log/nginx/sub1.mysite.com.access.log;

    # Make site accessible from http://localhost/
    location / {
        try_files $uri $uri/ /index.php?$uri&$args;
    }
    location ~ .php$ {
        root /var/www/sub1.mysite.com/public_html/sub-root;
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

知道我做错了什么吗?

【问题讨论】:

    标签: php cakephp nginx


    【解决方案1】:

    您的问题在于您的第一个 nginx 服务器指令:

    server {
       listen 80;
       server_name sub1.mysite.com;
       rewrite ^(.*) http://sub1.mysite.com$1 permanent;
    }
    

    您的指令正在侦听端口 80 (http) 并响应 sub1.mysite.com 然后它匹配所有 ^(.*) 并重定向到 http://sub1.mysite.com$1 permanent 其中 $1 是您匹配的结果,所以基本上,它附加了 @987654326 @。我看不出你为什么需要这个服务器指令?它侦听与您的主服务器指令相同的端口并响应相同的server_name 只需将其删除即可。结果应该是(注意我修复了一些设置,查看注释了解更多详细信息):

    server {
        listen   80; ## listen for ipv4; this line is default and implied
        server_name sub1.mysite.com; # removed the redundant 'name' keyword that you used here
        root /var/www/sub1.mysite.com/public_html/sub-root;
    
        index  index.php index.html index.htm;
    
        charset utf-8; # added this, you don't have to.
    
        # error_page 404 errors/404.html;
        access_log /var/log/nginx/sub1.mysite.com.access.log;
    
        # edited this. Given the request to `sub1.mysite.com` it will
        # try to first match the file itself, than an index file in the root
        # directory and then it will just send the request to index.php with the query string
        # which means the request to sub1.mysite.com/dashboard will be forwarded
        # to sub1.mysite.com/index.php/dashboard
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        # added both of these lines, you don't have to. they are just good practice.
        # If you don't add them your log files will get bloated real fast.
        # these files are requested by crawlers.
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
    
        location ~ \.php$ { # added a '\'
            # removed redundant 2 lines here
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    

    如果您对第一个服务器指令有其他意思,请告诉我。

    【讨论】:

    • 2 天前你在哪里......开玩笑的 :-)。说真的,非常感谢。它的工作!虽然,我现在遇到的唯一问题是index.php。如果我点击导航上的任何“漂亮网址”,它就可以工作;例如:如果我点击“仪表板”,它是sub1.mysite.com\dashboard,它可以正常转发.如果我直接去索引它工作正常sub1.mysite.com/index.php。但是,如果转到该站点而不指定索引文件 (sub1.mysite.com),我会收到错误 “缺少控制器错误:找不到 Sub1.mysite.comController。”。看起来它会将域名传递给蛋糕的控制器。有什么想法吗?
    • @user20719 检查我编辑的答案。你得到的错误是 CakePHP 错误?我从未使用过 CakePHP。无论如何,如果是 CakePHP 错误,则意味着您的 nginx 配置可能没问题,但您的路由声明存在问题。看看你的路由器