nginx对php三大框架的配置

TOC

一、ThinkPHP5框架

server {
    charset utf-8;
    client_max_body_size 128M;
    listen 80;
    server_name www.test.com;	  #指向域名
    root  /usr/tp5/public;   #指向地址
    index  index.php;

    location ~* \.(eot|otf|ttf|woff)$ {
        add_header Access-Control-Allow-Origin *;
    }

    location / {
        index    index.html index.php;
        if ( -f $request_filename) {
            break;
        }

        if ( !-e $request_filename) {
            rewrite ^/(.*)$ /index.php/$1 last;
            break;
        }
    }

    location ~ \.php {
        set $script $uri;
        set $path_info "";
        if ($uri ~ "^(.+\.php)(/.+)") {
            set $script $1;
            set $path_info $2;
        }
    include   fastcgi_params;
    fastcgi_index    index.php?IF_REWRITE=1;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_param    PATH_INFO    $path_info;
    fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param    SCRIPT_NAME    $script;
    try_files $uri =404;
    }
}

二、Laravel5框架

server {
    charset utf-8;
    client_max_body_size 128M;
    listen 80;
    server_name www.test.com;	  #指向域名
    root  /usr/laravel/public;  #指向地址
    index  index.php;

    location ~* \.(eot|otf|ttf|woff)$ {
        add_header Access-Control-Allow-Origin *;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include   fastcgi_params;
        fastcgi_index    index.php;
        fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass   127.0.0.1:9000;
        try_files $uri =404;
    }
}

三、Yii框架

server {
    charset utf-8;
    client_max_body_size 128M;
    listen 80;
    server_name www.test.com;	  #指向域名
    root  /usr/tp5/public;    #指向地址
    index  index.php;

    location ~* \.(eot|otf|ttf|woff)$ {
        add_header Access-Control-Allow-Origin *;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include   fastcgi_params;
        fastcgi_index    index.php;
        fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass   127.0.0.1:9000;
        try_files $uri =404;
    }
}

常规php配置

nginx.conf主配置文件

# nginx.conf
user root;
worker_processes 1;
worker_rlimit_nofile 1024000;

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

events {
    worker_connections 1024000;
    use epoll;
}

http {
    include /etc/nginx/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 /var/log/nginx/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    server_tokens off;
    server_names_hash_bucket_size 128;
    server_names_hash_max_size 512;
    keepalive_timeout 300;

    large_client_header_buffers 4 32k;
    client_header_buffer_size 32k;
    client_header_timeout 15s;
    client_body_timeout 15s;
    send_timeout 180s;

    open_file_cache max=102400 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 32k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types text/css text/xml application/javascript;
    gzip_vary on;

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

conf.d/default.conf副配置文件

# default.conf
server {
    listen 80;
    server_name _;
    root /var/www/html/public;
    index index.php;

    location ~ ^/favicon\.ico$ {
        root /var/www/html;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ ^/(images|javascript|js|css|flash|media|static)/ {
        expires 1d;
    }

    location = /status {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_cache off;
        #allow all;
        allow 127.0.0.1;
        deny all;
    }

    # include /var/www/html/remoteip.load;
    real_ip_header X-Forwarded-For;

    client_max_body_size 100m;
    client_body_buffer_size 128k;

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param HTTPS on;
        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;
        client_max_body_size 100m;

        add_header 'access-control-allow-origin' '*';
        add_header 'access-control-allow-methods' 'get, post, put, delete, options, patch';
        add_header 'access-control-allow-credentials' 'true';
        add_header 'access-control-allow-headers' 'accept, accept-encoding, accept-language, authorization, cache-control, connection, content-type, cookie, keep-alive, user-agent, token, app-language';

        if ($request_method = 'OPTIONS') {
            add_header 'access-control-max-age' 1728000;
            add_header 'content-type' 'text/plain charset=UTF-8';
            add_header 'content-length' 0;
            return 204;
        }
    }
    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log;
}