魏长东

weichangdong

东邪

Linux下再配置nginx和php

再次安装nginx和php-fpm,并架设网站。网站使用的CI框架的,域名是自己配置hosts访问的。

遇到的好多问题:

1是访问必须加index.php,否则根本定位不到网站目录上。

2是css,js,img资源无法访问。http://commonlogin.mobimagic.com/application/views/admin/styles/typecho.source.css这样的格式,居然没法访问,别的js和img都是一样的状况。

3.这种url没法访问,http://commonlogin.mobimagic.com/index.php/dashboard不能访问。

最后都一一解决了。

附上完整的nginx配置文件。

/etc/nginx/nginx.conf文件

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    upstream phpfastcgi {
        server 127.0.0.1:9000;
    }
    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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
#add by wcd
    charset utf-8;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 64k;
    large_client_header_buffers 4 64k;
    client_max_body_size 8m;
    keepalive_timeout  65;
    gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

/etc/nginx/conf.d/commonlogin.mobimagic.com.conf文件

server {
    listen       80;
    server_name  commonlogin.mobimagic.com;
    access_log  /var/log/nginx/commonlogin.access.log  main;

    index index.php;
	#解决/index/login没法访问的问题
    location / {
        index  index.htm index.html index.php;
        try_files $uri $uri/ /index.php?$args;
    }
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /data/app/commonlogin.mobimagic.com/;
        fastcgi_pass 127.0.0.1:9000;
        #rewrite ^/(js|javascript|html|images|styles)/(.*)$  /$1/$2 break;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
	# deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
	#解决静态资源没法访问的问题 add by wcd
    location ~/styles{
        root /data/app/commonlogin.mobimagic.com/;
    }
    location ~/javascript{
        root /data/app/commonlogin.mobimagic.com/;
    }
    location ~/images{
        root /data/app/commonlogin.mobimagic.com/;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires 30d;
    }
    location ~ .*\.(js|css)$ {
            expires 1h;
    }
}

当然这仅仅是实现了基本的功能,一些优化的参数之类的,完全没有考虑。