Nginx和php的結合

nginx_web發表於2012-06-20

 

 

在上面分別分析了NginxFastCGI模組和phpphp-fpm模式,下面通過一個例子將這兩者結合起來,使得動靜分離,靜態網頁由Nginx處理,而動態網頁由php-fpm處理,其實在本章一開始的部分就將這兩者結合起來了。

 

 

新增Nginx配置

 

下面看一個配置例項:

 

Nginx配置檔案nginx.conf

 

 

……

http {

    include       conf/mime.types;

    default_type  application/octet-stream;

    server_names_hash_bucket_size  128;

 

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

 

    fastcgi_buffer_size 64k;

    fastcgi_buffers 4 64k;

    fastcgi_busy_buffers_size 128k;

    fastcgi_temp_file_write_size 128k;

    fastcgi_connect_timeout 150;

    fastcgi_send_timeout 150;

    fastcgi_read_timeout 150;

 

    #keepalive_timeout  0;

    keepalive_timeout  20;

    tcp_nodelay        on;

 

    #gzip  on;

 

    server {

        listen       192.168.3.12:80; 

        server_name  xxx.com www.xxx.com; 

        #charset utf8;

 

        #access_log  logs/host.access.log  main;

 

        location = / {

            root   /sdb1/www/;  

            index  index.php;

        }

 

        location / {

            root   /sdb1/www/;

            index  index.php index.html;

 

            if (!-f $request_filename) {

                rewrite  ^(.*)$  /index.php?q=$1  last;

                break;

            }

 

            if (!-d $request_filename) {

                rewrite  ^(.*)$  /index.php?q=$1  last;

                break;

            }

 

        }

 

        error_page  404    /index.php;

 

        # static files directly

        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {

              access_log        off;

              expires           30d;

        }

 

        # redirect error pages to the static page /50x.html

        error_page  500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

 

        location ~ .php$ {

          fastcgi_pass   127.0.0.1:9001; 

          fastcgi_index  index.php;

          include fastcgi.conf;

        }

 

        # deny access to .htaccess files, if it’s a Apache's document root

        location ~ /\.ht {

            deny  all;

        }

 

     }

}

……

 

 

新增php-fpm配置

 

php-fpm的配置檔案php-fpm.conf:

   

[root@mail etc]# grep -v ";"  php-fpm.conf

 

[global]

pid = run/php-fpm.pid

error_log = log/php-fpm.log

log_level = debug

emergency_restart_threshold = 10

emergency_restart_interval = 20

process_control_timeout = 5

 

[www]

 

listen = 127.0.0.1:9000

listen.allowed_clients = 127.0.0.1

user = nobody

group = nobody

 

pm = dynamic

pm.max_children = 50

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 20

pm.max_requests = 300

 

pm.status_path = /status

ping.path = /ping

ping.response = pong

 

request_terminate_timeout = 50

request_slowlog_timeout = 50

 

slowlog = log/$pool.log.slow

rlimit_files = 1024

 

   

還有一個FastCGI的引數檔案fastcgi.conf就不貼出來了就是上面的那個內容。然後再重新載入Nginx的配置檔案就可以了。

 

本例項是在一個業務不太繁忙下的配置,如果你的業務較忙,那麼根據實際情況對配置檔案中的引數做相關調整就可以了,在前面的內容中已經將配置檔案中的指令介紹過了。

 

    有關使用upstream_keepalive模組提供FastCGI模組實現keep-live的知識請參考卷1,在此就不多羅嗦了。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27043155/viewspace-733320/,如需轉載,請註明出處,否則將追究法律責任。

相關文章