CodeIgniter和Nginx配置

时间:2015-02-22 23:44:40

标签: php codeigniter url nginx

我开发了我的CI应用程序并在超过4台Apache服务器上进行了测试,它运行良好。但是,我将它上传到新服务器Nginx,它有网址问题。谷歌搜索了很多,大致做了所有的事情,但没有结果。现在问题出在这里:

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

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

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我遇到了同样的问题。

我有一个网站 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。希望它能奏效。