2016-01-31 99 views
0

將我的應用程序移動到Nginx服務器之後,事情剛剛爆炸。 我的網站被加載,但是當我嘗試訪問一些控制器返回404找不到。 閱讀大量的文章如何配置nginx.conf但沒有成功。Nginx + Codeigniter重寫

這裏是我的nginx.conf

server { 
     listen  80; 
     server_name example.com; 

     root /var/www/html/travel; 
     index index.php; 

     # Enable rewrite error log 
     error_log /var/log/nginx/travel.error_log debug; 
     rewrite_log on; 

     # Any HTTP request other than those for assets folder, files folder and robots.txt 
     # is treated as a request for your index.php file. 

     location/{ 
      try_files $uri $uri/ /index.php?/$request_uri; 
     } 
     location ~* ^/(assets|files|robots\.txt) { } 

     # Deny access to .htaccess files, if Apache's document root 
     # concurs with Nginx's one 
     location ~ /\.ht { 
      deny all; 
     } 
    } 

奇怪的是,在error.log中沒有錯誤。

這裏是我笨的config.php

$config['base_url'] = 'http://example.com'; 
$config['index_page'] = ''; 
$config['uri_protocol'] = 'REQUEST_URI'; 
+1

錯誤日誌將列出導致404錯誤的路徑名。另外,你沒有PHP配置。你需要像'php-fpm'這樣的東西。見[這](https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/) –

+0

嗯,我做到了。在服務器內部添加這個內容:include fastcgi.conf;但是是一樣的 – IdlleF

+0

請重新上傳你的配置! – uzsolt

回答

1

檢查是否有幫助。替換兩個出現的<source_directory>;與您的正確值:

server { 
    listen  80 default_server; 
    listen  [::]:80 default_server; 
    server_name _; 
    root   <source_directory>; 
    rewrite_log on; 
    index index.html index.php; 

    # Load configuration files for the default server block. 
    include /etc/nginx/default.d/*.conf; 

    if ($host ~* ^www\.(.*)) 
    { 
     set $host_without_www $1; 
     rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent; 
    } 

    # canonicalize codeigniter url end points 
    # if your default controller is something other than "welcome" you should change the following 
    if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$) 
    { 
     rewrite ^(.*)$/permanent; 
    } 

    # removes trailing "index" from all controllers 
    if ($request_uri ~* index/?$) 
    { 
     rewrite ^/(.*)/index/?$ /$1 permanent; 
    } 

    # removes trailing slashes (prevents SEO duplicate content issues) 
    if (!-d $request_filename) 
    { 
     rewrite ^/(.+)/$ /$1 permanent; 
    } 

    # removes access to "system" folder, also allows a "System.php" controller 
    if ($request_uri ~* ^/system) 
    { 
     rewrite ^/(.*)$ /index.php?/$1 last; 
     break; 
    } 

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap 
    if (!-e $request_filename) 
    { 
     rewrite ^/(.*)$ /index.php?/$1 last; 
     break; 
    } 

    # use fastcgi for all php files 
    location ~ \.php$ 
    { 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME <source_directory>$fastcgi_script_name; 
     include fastcgi_params; 
    } 

    # deny access to apache .htaccess files 
    location ~ /\.htaccess 
    { 
     deny all; 
    } 

    error_page 404 /404.html; 
    location = /40x.html 
    { 
    } 

    error_page 500 502 503 504 /50x.html; 
    location = /50x.html 
    { 
    } 
} 
+0

這個文件應放在哪裏?我在第三方託管服務提供商的生產環境中 –

+0

@TheOnlySmartBoy這應該放在nginx配置文件裏面,它通常存儲在/etc/nginx/nginx.conf中 – Klaus

0
server { 
      server_name domain.tld; 

      root /var/www/html/travel; 
      index index.php; 

      # set expiration of assets to MAX for caching 
      location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ { 
        expires max; 
        log_not_found off; 
      } 

      location/{ 
        # Check if a file or directory index file exists, else route it to index.php. 
        try_files $uri $uri/ /index.php; 
      } 

      location ~* \.php$ { 
        fastcgi_pass 127.0.0.1:9000; 
        include fastcgi.conf; 
      } 
    } 

這是我的配置有了一些變化。