nginx cakephp虚拟主机重定向

时间:2016-07-27 19:33:46

标签: php cakephp nginx

我是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;
    }
}

知道我做错了吗?

1 个答案:

答案 0 :(得分:0)

您的问题在于您的第一个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是您的结果基本匹配,它附加sub1.mysite.com。我没有看到你需要这个服务器指令的原因吗?它侦听与主服务器指令相同的端口并响应相同的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;
    }
}

如果你对第一个服务器指令有另一种意思,请告诉我。