1.nginx.conf配置文件如下
#用户 用户组
#user www www;
#工作进程,根据硬件调整,有人说几核cpu,就配几个,我觉得可以多一点
worker_processes 4;
#错误日志
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid文件位置
#pid logs/nginx.pid;
#工作进程的最大连接数量,根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#日志的格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#访问日志
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
server
{
listen 85;
server_name 192.168.55.88;
root /usr/local/www/nginx/icntv;
index index.php index.html index.htm;
#蓝色部分等价绿色部分,请选择一种注释另一种即可
if ($request_filename !~ /(themes|jscript|css|images|fckeditor|data|robot\.txt|index\.php))
{
rewrite ^/(.*)$ /index.php?$1 last;
}
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?$1 last;
break;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|mp4|flv)$
{
expires 30d;
}
location ~ .*\.(js|css|jscript)?$
{
expires 1h;
}
#所有php后缀的,都通过fastcgi发送到9000端口上
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/www/nginx/icntv/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht
{
deny all;
}
}
server
{
listen 80;
server_name localhost;
root /usr/local/www/nginx/;
index index.php index.html index.htm;
# catch all
error_page 404 /index.php;
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/www/nginx/$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
}
server
{
listen 81;
server_name 192.168.55.88;
error_log /root/nginx_error.log error;
root /usr/local/www/nginx/icntv/;
index index.php index.html index.htm;
# enforce www (exclude certain subdomains)
#if ($host !~ ^(www|subdomain))
#{
#rewrite ^/(.*)$ $scheme:// permanent;
#}
# enforce NO www
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 slashes from all 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;
}
# catch all
error_page 404 /index.php;
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/www/nginx/icntv/$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
}
}
2.CodeIgniter配置注意事项如下:
$config['base_url'] = '';//注意与nginx中设置的server_name保持一致
$config['index_page'] = '';//从url中隐藏index.php文件
$config['allow_get_array'] = TRUE;
$config['enable_query_strings'] = FALSE;//关闭查询字符串形式的url路由
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
$config['directory_trigger'] = 'd'; // experimental not currently in use
3.问题:
1)81端口尚未测试(与85端口不同的部分可能会出现问题)
2)如果出现502 bad gateway错误则可能需要启动php-cgi,请参考本博客【FreeBSD下spawn-fcgi启动与关闭】,重新启动fcgi
3)关于以上正则匹配中出现的匹配符~*(网上参考时很多出现这个匹配符号),个人理解nginx中配置时是以perl兼容的正则表达式方式匹配的,所以,正确写法应该是~,经测试发现两者都不影响服务器运行,即都有效,这是目前本人尚不清楚的地方...有知道的大大请帮忙解答下
4)关于fcgi不自动启动的疑问尚未解决,即以上配置中的这句fastcgi_pass 127.0.0.1:9000;在配置完后重新启动nginx(service nginx restart)时用sockstat -l命令或netstat -anl命令查看本地监听的端口时,并未发现9000端口已开启监听