【问题标题】:CodeIgniter and Nginx configurationsCodeIgniter 和 Nginx 配置
【发布时间】:2015-02-22 23:44:40
【问题描述】:

我开发了我的 CI 应用程序并在超过 4 个 Apache 服务器上对其进行了测试,它运行良好。但是在我将它上传到 Nginx 的新服务器之后,它的 url 有问题。我用谷歌搜索了很多,几乎做了每件事,但没有结果。现在问题来了:

由于项目结构和规模,我需要每个网址都像这样:

http://example.com/myappname/controller/action 例如:http://example.com/myappname/auth/login

但它不起作用。起作用的是: http://example.com/myappname/index.php?/auth/login 这不是我想要的。

nginx的配置是:

server {
listen 80;
listen [::]:80;

root /var/nginx/www/mydomain.com/www;
index index.php index.html index.htm;

server_name mydomain.com www.mydomain.com;

location / {
        try_files $uri $uri/ /index.php?/$request_uri;
}   
error_page 500 502 503 504 /50x.html;
location = /50x.html {
    root /usr/share/nginx/html;
}
location ~ \.php$ {

    # With php5-fpm:
            try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

location ~ /\.ht {
    deny all;
}
location ~* \.(png|jpg|jpeg|gif|ico|ttf|woff)(\?ver=[0-9.]+)?$ {
    expires 1w;
}
location ~* \.(css|js|html)(\?ver=[0-9.]+)?$ {
    expires 1w;
}   

}

【问题讨论】:

    标签: php codeigniter url nginx


    【解决方案1】:

    您的 codeIgniter config.php 包含以下信息:

    $config['base_url'] = "http://domain.tld/";
    $config['index_page']   = "";
    $config['uri_protocol'] = "REQUEST_URI";
    

    这里是nginx重写规则,

    server {
        server_name domain.tld;
    
        root /var/www/codeignitor;
        index index.html 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$ {
            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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。

      我有一个网站 http://website.com

      和codeigniter文件夹 http://website.com/partners.

      我只是将 nginx 配置更改为专门用于此 codeigniter 文件夹的位置。

      我是这样添加的。

      location / {
                  try_files $uri $uri/ /index.php;
              }
      
      location /partners {
                  try_files $uri $uri/ /partners/index.php;
              }
      

      至于你的情况。我认为这个设置应该如下所示:

      server {
      listen 80;
      listen [::]:80;
      
      root /var/nginx/www/mydomain.com/www;
      index index.php index.html index.htm;
      
      server_name mydomain.com www.mydomain.com;
      
      location / {
              try_files $uri $uri/ /index.php;
      } 
      
      #ADD THIS LINE
      location /myappname {
              try_files $uri $uri/ /myappname/index.php;
      } 
      
      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
          root /usr/share/nginx/html;
      }
      location ~ \.php$ {
      
          # With php5-fpm:
                  try_files $uri =404;
          fastcgi_pass unix:/var/run/php5-fpm.sock;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }
      
      location ~ /\.ht {
          deny all;
      }
      location ~* \.(png|jpg|jpeg|gif|ico|ttf|woff)(\?ver=[0-9.]+)?$ {
          expires 1w;
      }
      location ~* \.(css|js|html)(\?ver=[0-9.]+)?$ {
          expires 1w;
      }  
      

      确保通过运行 sudo nginx -t 测试一切正常,然后重新启动 nginx。希望它会奏效。

      【讨论】:

        猜你喜欢
        • 2011-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-15
        • 2014-07-23
        • 2014-11-03
        • 1970-01-01
        相关资源
        最近更新 更多