CodeIgniter 的nginx配置示例

1. nginx的基本配置

server {
    listen  80;
    server_name codeigniter.localhost;
    root /var/www/html/codeigniter;
    index index.php index.html;
    access_log    /tmp/nginx_access.log;
    # 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$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_read_timeout 600; 
    }
}

2. 增長最大鏈接數,支持高併發

設置nginx的最大鏈接數,以及多核CPU設置。php

worker_processes  4;  
worker_cpu_affinity 0001 0010 0100 1000;
worker_rlimit_nofile 102400;

events {
    worker_connections 102400;
    # multi_accept on;
}

解決錯誤:css

*4645 socket() failed (24: Too many open files)html

增大nginx能夠接受的併發數,以應對高併發。nginx

訪問量高時,因爲系統對於進程的最大文件打開數的限制(ulimit -n 默認1024),而nginx屬於單進程多線程併發的服務,因此在訪問量高時,鏈接數超過1024後,會被系統限制鏈接。因此還須要設置ulimit的支持的最大文件描述符。bash

在文件:/etc/security/limits.conf 中增長下面的兩行配置多線程

 * soft  nofile 655360
 * hard nofile 655360

而後重啓生效, ulimit -a 查看能夠支持的文件數。併發

 

3. PHP-FPM 的設置

爲PHP-FPM設置最大鏈接數。socket

這一塊寫的比較虛,不少參數我也尚未弄得很明白,內存夠用的時候在必定的程度上能夠增長併發數。codeigniter

pm.mac_children的值須要根據狀況來定。在個人Ubuntu 16.04下面,修改如下兩個文件:高併發

/etc/php/7.0/fpm/php-fpm.conf

process.max = 300
rlimit_files = 8192

 /etc/php/7.0/fpm/pool.d/www.conf

pm = static
pm.max_children = 200
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35

 

4. 性能測試

個人虛擬機Ubuntu 16.04 LTS的硬件性能以下:

四核 Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz 

4GB 內存

 測試locahost的hello.html 的結果以下:

A.  ab -c 100 -n 1000 http://localhost:8000/hello.html
hello.html的代碼

hello world

測試結果以下: 

Requests per second:    10389.29 [#/sec] (mean)
Time per request:       9.625 [ms] (mean)
Time per request:       0.096 [ms] (mean, across all concurrent requests)
Transfer rate:          2546.59 [Kbytes/sec] received

B. ab -c 100 -n 1000 http://localhost:8000/hello.php

hello.php的代碼以下:

<?php
echo "hello world";

測試結果以下: 

Requests per second:    5154.29 [#/sec] (mean)
Time per request:       19.401 [ms] (mean)
Time per request:       0.194 [ms] (mean, across all concurrent requests)
Transfer rate:          790.26 [Kbytes/sec] received

C. ab -c 100 -n 1000 http://codeigniter.localhost:8000/welcome/hello

CI 3.0 的穩定版的,在Welcome這個controller下面實現hello方法:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {
    public function index()
    {   
        echo "hello";
    }   
    public function hello()
    {   
        echo "hello world";
    }   
}

測試結果以下:

Requests per second:    1652.19 [#/sec] (mean)
Time per request:       60.526 [ms] (mean)
Time per request:       0.605 [ms] (mean, across all concurrent requests)
Transfer rate:          253.31 [Kbytes/sec] received
相關文章
相關標籤/搜索