nginx与php怎么实现高并发


本文摘自PHP中文网,作者(*-*)浩,侵删。

高性能服务器通过配置nginx和php-fpm,代替docker容器技术,实现请求服务高并发处理。

主要说明配置主要参数。

nginx主要配置

通过nginx实现php-fpm服务器负载,用户访问服务时,将请求分配给不同的php-fpm服务器。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

# nginx启动worker进程数

worker_processes auto;

 

#Changes the limit on the maximum number of open files (RLIMIT_NOFILE) for worker processes.定义进程可以打开的最大文件数,与linux内核相同即可

worker_rlimit_nofile 65535;

# 定义worker进程的优先级,数字越小优先级越高 [-20,19]

worker_priority -20;

 

events{

    # The maximum number of connections that each worker process can handle simultaneously.每个进程同时处理的连接数

    worker_connections 10240;

    # If multi_accept is disabled, a worker process will accept one new connection at a time. Otherwise, a worker process will accept all new connections at a time.

    multi_accept on;

}

 

http{

    access_log /var/log/nginx/access.log main buffer=32k;

     

    # Nginx将使用sendfile内核来调用处理文件传递。

    sendfile on;

     

    # 根据权重分配请求到不同服务器,以下配置,当有6个请求时,5个发送到9000端口服务器,1个发送到9001端口服务器

    upstream phpload{

        server 127.0.0.1:9000 weight=6;

        server 127.0.0.1:9001 weight=1;

    }

     

    server{

        listen 443;

         

        root /data/www/webserver;

        index index.php;

         

        location / {

            if (!-e $request_filename) {

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

                break;

            }

        }

         

        location ~ .php($|/){

            set $script $uri;

            set $path_info "";

 

            if ($uri ~ "^(.+.php)(/.+)") {

                set $script $1;

                set $path_info $2;

            }

 

            fastcgi_param SCRIPT_FILENAME $document_root$script;

            fastcgi_param SCRIPT_NAME $script;

            fastcgi_param PATH_INFO $path_info;

 

            try_files $uri =404;

             

            # 使用负载,分发请求到上游php服务器

            fastcgi_pass  myfastcgi;

            fastcgi_index index.php;

            include       fastcgi_params;

        }

    }

}

php-FPM是一个PHP FastCGI的管理器,它实际上就是PHP源代码的补丁,旨在将FastCGI进程管理引进到PHP软件包中,我们必须将其patch到PHP源代码中,然后再行编译才能使用。而现在我们可以在PHP 5.3.2及更新版本中直接开启并使用即可,因为PHP从该版本已经将其收入到软件包中,所以其不再是补丁包的存在了。

阅读剩余部分

相关阅读 >>

nginx与php怎么实现高并发

nginx服务器上,master进程和worker进程分别是什么

nginx的好处有哪些

nginx访问日志在哪里

nginx作用是什么意思

nginx常用命令介绍

nginx是web服务器吗

nginx创建多个配置文件的方法

centos下nginx无法访问怎么办

编译安装nginx却requires the pcre library

更多相关阅读请进入《nginx》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...