2016-07-27 85 views
1

我是新來的nginx的,不能讓我的網站的工作(上一個AWS CakePHP的網站)。我得到的消息「頁面沒有正確重定向」,以及一些如何將域名附加到自身(重定向循環);CakePHP的nginx的虛擬主機重定向

例如,當我進入我的網站sub1.mysite.com的增加的browers和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; 
    } 
} 

任何想法,我做錯了嗎?

回答

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

如果你在你的心中還有另外一個平均的第一臺服務器的指令,讓我知道。

+0

你在哪裏3天前...只是在開玩笑:-)。真的,非常感謝你。它的工作!雖然,我現在具有唯一的問題是'index.php'.If我點擊任何一個「漂亮的URL」的於它的工作NAV;例如:如果我點擊「儀表板」,這是'sub1.mysite。 com \ dashboard',它轉發很好。如果我直接進入索引,它可以很好地運行'sub1.mysite.com/index.php'。但是,如果去到現場和未指定索引文件(sub1.mysite.com)我得到錯誤**「缺少控制器錯誤:Sub1.mysite.comController找不到。」 **。看起來像它將域名傳遞給蛋糕的控制器。任何想法? – user20719

+0

@ user20719檢查我編輯的答案。你得到的錯誤是CakePHP錯誤?我從來沒有用過CakePHP。無論如何,如果它是一個CakePHP錯誤,那麼這意味着你的nginx配置可能是好的,但是你的路由聲明存在問題。看看你的路由器 – kfirba