1、Nginx + php-fpm 在 Windows 10 下的情况表现,当在一个请求(http://api.channel-pub-api.localhost)中 CURL 另一个请求(http://api.channel-pub-api.localhost)时,报错:fopen(http://api.channel-pub-api.localhost/qq/v1/articles/standard?group_id=015ce30b116ce86058fa6ab4fea4ac63): failed to open stream: HTTP request failed! 。如图1

图1

{
    "name": "HTTP Client Exception",
    "message": "fopen(http://api.channel-pub-api.localhost/qq/v1/articles/standard?group_id=015ce30b116ce86058fa6ab4fea4ac63): failed to open stream: HTTP request failed! ",
    "code": 2,
    "type": "yii\\httpclient\\Exception",
    "file": "E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2-httpclient\\src\\StreamTransport.php",
    "line": 68,
    "stack-trace": [
        "#0 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2-httpclient\\src\\Transport.php(41): yii\\httpclient\\StreamTransport->send(Object(yii\\httpclient\\Request))",
        "#1 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2-httpclient\\src\\Client.php(259): yii\\httpclient\\Transport->batchSend(Array)",
        "#2 E:\\wwwroot\\channel-pub-api-feature-task-group\\common\\logics\\http\\channel_pub_api\\Article.php(55): yii\\httpclient\\Client->batchSend(Array)",
        "#3 E:\\wwwroot\\channel-pub-api-feature-task-group\\common\\services\\TaskGroupService.php(117): common\\logics\\http\\channel_pub_api\\Article->batchPostArticlesStandard('015ce30b116ce86...', Array)",
        "#4 E:\\wwwroot\\channel-pub-api-feature-task-group\\api\\filters\\TaskGroupFilter.php(45): common\\services\\TaskGroupService::createMultipleSync(Array)",
        "#5 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\base\\ActionFilter.php(92): api\\filters\\TaskGroupFilter->afterAction(Object(api\\rests\\task_group\\CreateAction), Array)",
        "#6 [internal function]: yii\\base\\ActionFilter->afterFilter(Object(yii\\base\\ActionEvent))",
        "#7 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\base\\Component.php(627): call_user_func(Array, Object(yii\\base\\ActionEvent))",
        "#8 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\base\\Controller.php(305): yii\\base\\Component->trigger('afterAction', Object(yii\\base\\ActionEvent))",
        "#9 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\rest\\Controller.php(76): yii\\base\\Controller->afterAction(Object(api\\rests\\task_group\\CreateAction), Array)",
        "#10 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\base\\Controller.php(159): yii\\rest\\Controller->afterAction(Object(api\\rests\\task_group\\CreateAction), Array)",
        "#11 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\base\\Module.php(528): yii\\base\\Controller->runAction('create', Array)",
        "#12 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\web\\Application.php(103): yii\\base\\Module->runAction('v1/task-group/c...', Array)",
        "#13 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\base\\Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))",
        "#14 E:\\wwwroot\\channel-pub-api-feature-task-group\\api\\web\\index.php(17): yii\\base\\Application->run()",
        "#15 {main}"
    ],
    "previous": {
        "name": "PHP Warning",
        "message": "fopen(http://api.channel-pub-api.localhost/qq/v1/articles/standard?group_id=015ce30b116ce86058fa6ab4fea4ac63): failed to open stream: HTTP request failed! ",
        "code": 2,
        "type": "yii\\base\\ErrorException",
        "file": "E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2-httpclient\\src\\StreamTransport.php",
        "line": 61,
        "stack-trace": [
            "#0 [internal function]: yii\\base\\ErrorHandler->handleError(2, 'fopen(http://ap...', 'E:\\\\wwwroot\\\\chan...', 61, Array)",
            "#1 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2-httpclient\\src\\StreamTransport.php(61): fopen('http://api.chan...', 'rb', false, Resource id #205)",
            "#2 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2-httpclient\\src\\Transport.php(41): yii\\httpclient\\StreamTransport->send(Object(yii\\httpclient\\Request))",
            "#3 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2-httpclient\\src\\Client.php(259): yii\\httpclient\\Transport->batchSend(Array)",
            "#4 E:\\wwwroot\\channel-pub-api-feature-task-group\\common\\logics\\http\\channel_pub_api\\Article.php(55): yii\\httpclient\\Client->batchSend(Array)",
            "#5 E:\\wwwroot\\channel-pub-api-feature-task-group\\common\\services\\TaskGroupService.php(117): common\\logics\\http\\channel_pub_api\\Article->batchPostArticlesStandard('015ce30b116ce86...', Array)",
            "#6 E:\\wwwroot\\channel-pub-api-feature-task-group\\api\\filters\\TaskGroupFilter.php(45): common\\services\\TaskGroupService::createMultipleSync(Array)",
            "#7 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\base\\ActionFilter.php(92): api\\filters\\TaskGroupFilter->afterAction(Object(api\\rests\\task_group\\CreateAction), Array)",
            "#8 [internal function]: yii\\base\\ActionFilter->afterFilter(Object(yii\\base\\ActionEvent))",
            "#9 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\base\\Component.php(627): call_user_func(Array, Object(yii\\base\\ActionEvent))",
            "#10 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\base\\Controller.php(305): yii\\base\\Component->trigger('afterAction', Object(yii\\base\\ActionEvent))",
            "#11 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\rest\\Controller.php(76): yii\\base\\Controller->afterAction(Object(api\\rests\\task_group\\CreateAction), Array)",
            "#12 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\base\\Controller.php(159): yii\\rest\\Controller->afterAction(Object(api\\rests\\task_group\\CreateAction), Array)",
            "#13 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\base\\Module.php(528): yii\\base\\Controller->runAction('create', Array)",
            "#14 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\web\\Application.php(103): yii\\base\\Module->runAction('v1/task-group/c...', Array)",
            "#15 E:\\wwwroot\\channel-pub-api-feature-task-group\\vendor\\yiisoft\\yii2\\base\\Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))",
            "#16 E:\\wwwroot\\channel-pub-api-feature-task-group\\api\\web\\index.php(17): yii\\base\\Application->run()",
            "#17 {main}"
        ]
    }
}

2、编辑 Nginx 配置文件,复制 server:API 为 server:LOCALHOST API。调整 listen 81、server_name localhost、fastcgi_pass 127.0.0.1:9001。总计 3 项配置值。如图2、图3

图2

 

图3

## FRONTEND ##
server {
    charset utf-8;

 client_max_body_size 200m;
 client_body_buffer_size 1024k;

 fastcgi_read_timeout 180s;

    listen 80; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name www.channel-pub-api.localhost www.channel-pub-api-localhost.chinamcloud.cn auth.channel-pub.wjdev.chinamcloud.cn;
    root        E:/wwwroot/channel-pub-api-feature-task-group/frontend/web;
    index       index.php;

    access_log  logs/www.channel-pub-api.localhost.access.log;
    error_log   logs/www.channel-pub-api.localhost.error.log;

 location / {
  # Redirect everything that isn't a real file to index.php
  try_files $uri $uri/ /index.php$is_args$args;
 }

 # uncomment to avoid processing of calls to non-existing static files by Yii
 #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
 #    try_files $uri =404;
 #}
 #error_page 404 /404.html;

 # deny accessing php files for the /assets directory
 location ~ ^/assets/.*\.php$ {
  deny all;
 }

 location ~ \.php$ {
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_pass 127.0.0.1:9000;
  #fastcgi_pass unix:/var/run/php5-fpm.sock;
  try_files $uri =404;
 }

 location ~* /\. {
  deny all;
 }
}

## API ##
server {
    charset utf-8;

 client_max_body_size 200m;
 client_body_buffer_size 1024k;

 fastcgi_read_timeout 180s;

    listen 80; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name api.channel-pub-api.localhost api.channel-pub-api-localhost.chinamcloud.cn;

    root E:/wwwroot/channel-pub-api-feature-task-group;
    index index.php;

    access_log  logs/api.channel-pub-api.localhost.access.log;
    error_log   logs/api.channel-pub-api.localhost.error.log;

 location / {
        root E:/wwwroot/channel-pub-api-feature-task-group/api/web;
        try_files $uri $uri/ /api/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /baijia {
        alias E:/wwwroot/channel-pub-api-feature-task-group/baijia/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /baijia/ {
        #    return 301 /baijia;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /baijia {
            # if your location is "/baijia", try use "/baijia/baijia/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /baijia/baijia/web/index.php$is_args$args;
        }

        # if your location is "/baijia", try use "/baijia/baijia/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /baijia/baijia/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/baijia/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/baijia/assets/.+\.php(/|$) {
            deny all;
        }
    }

 location /customize {
        alias E:/wwwroot/channel-pub-api-feature-task-group/customize/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /customize/ {
        #    return 301 /customize;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /customize {
            # if your location is "/customize", try use "/customize/customize/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /customize/customize/web/index.php$is_args$args;
        }

        # if your location is "/customize", try use "/customize/customize/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /customize/customize/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/customize/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/customize/assets/.+\.php(/|$) {
            deny all;
        }
    }

 location /netease {
        alias E:/wwwroot/channel-pub-api-feature-task-group/netease/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /netease/ {
        #    return 301 /netease;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /netease {
            # if your location is "/netease", try use "/netease/netease/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /netease/netease/web/index.php$is_args$args;
        }

        # if your location is "/netease", try use "/netease/netease/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /netease/netease/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/netease/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/netease/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /qq {
        alias E:/wwwroot/channel-pub-api-feature-task-group/qq/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /qq/ {
        #    return 301 /qq;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /qq {
            # if your location is "/qq", try use "/qq/qq/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /qq/qq/web/index.php$is_args$args;
        }

        # if your location is "/qq", try use "/qq/qq/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /qq/qq/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/qq/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/qq/assets/.+\.php(/|$) {
            deny all;
        }
    }

 location /weibo {
        alias E:/wwwroot/channel-pub-api-feature-task-group/weibo/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /weibo/ {
        #    return 301 /weibo;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /weibo {
            # if your location is "/weibo", try use "/weibo/weibo/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /weibo/weibo/web/index.php$is_args$args;
        }

        # if your location is "/weibo", try use "/weibo/weibo/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /weibo/weibo/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/weibo/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/weibo/assets/.+\.php(/|$) {
            deny all;
        }
    }

 location /wx {
        alias E:/wwwroot/channel-pub-api-feature-task-group/wx/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /wx/ {
        #    return 301 /wx;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /wx {
            # if your location is "/wx", try use "/wx/wx/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /wx/wx/web/index.php$is_args$args;
        }

        # if your location is "/wx", try use "/wx/wx/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /wx/wx/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/wx/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/wx/assets/.+\.php(/|$) {
            deny all;
        }
    }

 location ~ ^/.+\.php(/|$) {
        rewrite (?!^/((api|baijia|customize|netease|qq|weibo|wx)/web|baijia|customize|netease|qq|weibo|wx))^ /api/web$uri break;
  rewrite (?!^/baijia/web)^/baijia(/.+)$ /baijia/web$1 break;
  rewrite (?!^/customize/web)^/customize(/.+)$ /customize/web$1 break;
        rewrite (?!^/netease/web)^/netease(/.+)$ /netease/web$1 break;
  rewrite (?!^/qq/web)^/qq(/.+)$ /qq/web$1 break;
  rewrite (?!^/weibo/web)^/weibo(/.+)$ /weibo/web$1 break;
  rewrite (?!^/wx/web)^/wx(/.+)$ /wx/web$1 break;

  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_pass 127.0.0.1:9000;
  #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $fastcgi_script_name =404;
    }

    location ~ /\. {
        deny all;
    }
}

## LOCALHOST API ##
server {
    charset utf-8;

 client_max_body_size 200m;
 client_body_buffer_size 1024k;

 fastcgi_read_timeout 180s;

    listen 81; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name localhost;

    root E:/wwwroot/channel-pub-api-feature-task-group;
    index index.php;

    access_log  logs/localhost.access.log;
    error_log   logs/localhost.error.log;

 location / {
        root E:/wwwroot/channel-pub-api-feature-task-group/api/web;
        try_files $uri $uri/ /api/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /baijia {
        alias E:/wwwroot/channel-pub-api-feature-task-group/baijia/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /baijia/ {
        #    return 301 /baijia;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /baijia {
            # if your location is "/baijia", try use "/baijia/baijia/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /baijia/baijia/web/index.php$is_args$args;
        }

        # if your location is "/baijia", try use "/baijia/baijia/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /baijia/baijia/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/baijia/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/baijia/assets/.+\.php(/|$) {
            deny all;
        }
    }

 location /customize {
        alias E:/wwwroot/channel-pub-api-feature-task-group/customize/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /customize/ {
        #    return 301 /customize;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /customize {
            # if your location is "/customize", try use "/customize/customize/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /customize/customize/web/index.php$is_args$args;
        }

        # if your location is "/customize", try use "/customize/customize/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /customize/customize/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/customize/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/customize/assets/.+\.php(/|$) {
            deny all;
        }
    }

 location /netease {
        alias E:/wwwroot/channel-pub-api-feature-task-group/netease/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /netease/ {
        #    return 301 /netease;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /netease {
            # if your location is "/netease", try use "/netease/netease/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /netease/netease/web/index.php$is_args$args;
        }

        # if your location is "/netease", try use "/netease/netease/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /netease/netease/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/netease/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/netease/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /qq {
        alias E:/wwwroot/channel-pub-api-feature-task-group/qq/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /qq/ {
        #    return 301 /qq;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /qq {
            # if your location is "/qq", try use "/qq/qq/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /qq/qq/web/index.php$is_args$args;
        }

        # if your location is "/qq", try use "/qq/qq/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /qq/qq/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/qq/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/qq/assets/.+\.php(/|$) {
            deny all;
        }
    }

 location /weibo {
        alias E:/wwwroot/channel-pub-api-feature-task-group/weibo/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /weibo/ {
        #    return 301 /weibo;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /weibo {
            # if your location is "/weibo", try use "/weibo/weibo/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /weibo/weibo/web/index.php$is_args$args;
        }

        # if your location is "/weibo", try use "/weibo/weibo/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /weibo/weibo/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/weibo/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/weibo/assets/.+\.php(/|$) {
            deny all;
        }
    }

 location /wx {
        alias E:/wwwroot/channel-pub-api-feature-task-group/wx/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /wx/ {
        #    return 301 /wx;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /wx {
            # if your location is "/wx", try use "/wx/wx/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /wx/wx/web/index.php$is_args$args;
        }

        # if your location is "/wx", try use "/wx/wx/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /wx/wx/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/wx/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/wx/assets/.+\.php(/|$) {
            deny all;
        }
    }

 location ~ ^/.+\.php(/|$) {
        rewrite (?!^/((api|baijia|customize|netease|qq|weibo|wx)/web|baijia|customize|netease|qq|weibo|wx))^ /api/web$uri break;
  rewrite (?!^/baijia/web)^/baijia(/.+)$ /baijia/web$1 break;
  rewrite (?!^/customize/web)^/customize(/.+)$ /customize/web$1 break;
        rewrite (?!^/netease/web)^/netease(/.+)$ /netease/web$1 break;
  rewrite (?!^/qq/web)^/qq(/.+)$ /qq/web$1 break;
  rewrite (?!^/weibo/web)^/weibo(/.+)$ /weibo/web$1 break;
  rewrite (?!^/wx/web)^/wx(/.+)$ /wx/web$1 break;

  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_pass 127.0.0.1:9001;
  #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $fastcgi_script_name =404;
    }

    location ~ /\. {
        deny all;
    }
}

## MISC ##

### WWW Redirect ###
server {
    listen       80;
    server_name  channel-pub-api.localhost;
    return       301 http://www.channel-pub-api.localhost$request_uri;
}

3、新开 PowerShell 窗口,执行 php-cgi.exe -b 127.0.0.1:9001-c C:/php-7.2.14/php.ini,如图4

图4

4、Nginx + php-fpm 在 Windows 10 下的情况表现,当在一个请求(http://api.channel-pub-api.localhost)中 CURL 另一个请求(http://localhost:81)时,响应成功,如图5

图5

5、查看具体的请求发起时间节点,虽然是并行请求,但是实际表现上却是串行的。即一个请求完毕后,再执行下一个请求。其总体响应时间长度为:21.75s。如图6

图6

10:37:26.340 info yii\httpclient\StreamTransport::send POST http://localhost:81/baijia/v1/articles/standard?group_id=015ce30b116ce86058fa6ab4fea4ac62
Content-Type: application/json; charset=UTF-8

10:37:29.461 info yii\httpclient\StreamTransport::send POST http://localhost:81/netease/v1/articles/standard?group_id=015ce30b116ce86058fa6ab4fea4ac62
Content-Type: application/json; charset=UTF-8

10:37:32.732 info yii\httpclient\StreamTransport::send POST http://localhost:81/weibo/v1/articles/toutiao?group_id=015ce30b116ce86058fa6ab4fea4ac62
Content-Type: application/json; charset=UTF-8

10:37:35.757 info yii\httpclient\StreamTransport::send POST http://localhost:81/qq/v1/articles/standard?group_id=015ce30b116ce86058fa6ab4fea4ac62
Content-Type: application/json; charset=UTF-8

10:37:38.533 info yii\httpclient\StreamTransport::send POST http://localhost:81/qq/v1/articles/standard?group_id=015ce30b116ce86058fa6ab4fea4ac62
Content-Type: application/json; charset=UTF-8

10:37:41.132 info yii\httpclient\StreamTransport::send POST http://localhost:81/wx/v1/wx-articles/article?group_id=015ce30b116ce86058fa6ab4fea4ac62
Content-Type: application/json; charset=UTF-8

10:37:44.207 info yii\httpclient\StreamTransport::send POST http://localhost:81/customize/v1/articles/standard?group_id=015ce30b116ce86058fa6ab4fea4ac62
Content-Type: application/json; charset=UTF-8

6、决定分析测试 Nginx + php-fpm 在 Windows 10 下的串行表现。新建 phpinfo.php(延时 30 秒写入日志)、phpinfo1.php(延时 5 秒写入日志),在浏览器中先打开 phpinfo.php、然后再迅速打开 phpinfo1.php,间隔时间约为 1 秒左右。
phpinfo.php

<?php
sleep(30);
file_put_contents('E:/wwwroot/channel-pub-api/console/runtime/logs/sleep-0-' . time() . '.txt', time());
phpinfo();
?>

phpinfo1.php

<?php
sleep(5);
file_put_contents('E:/wwwroot/channel-pub-api/console/runtime/logs/sleep-1-' . time() . '.txt', time());
phpinfo();
?>

7、在打开 phpinfo.php 后,其大约加载了 30 秒左右的时间长度。在打开 phpinfo1.php 后,其大约加载了 35 秒左右的时间长度(如果支持并行,则加载时间预计为 5 秒左右。如果不支持并行,仅支持串行,则加载时间预计为 35 秒左右,其中 30 秒为等待 phpinfo.php 运行结束的时间,只有当 phpinfo.php 运行结束后,9001 端口才可用,5 秒为 phpinfo1.php 自身运行的时间)。如图7

图7

8、分别查看生成的日志文件的创建时间。可以确定 phpinfo1.php 的日志文件的创建时间晚于 phpinfo.php 的日志文件的创建时间,精确为 5 秒钟。由此可以确定,Nginx + php-fpm 在 Windows 10 下,是仅支持串行请求的。如图8

图8

sleep-0-1585200354.txt
sleep-1-1585200359.txt

9、开发环境的部署方案为:CentOS + Nginx + php-fpm。重复第 6、7、8 步骤。分别查看生成的日志文件的创建时间。可以确定 phpinfo1.php 的日志文件的创建时间早于 phpinfo.php 的日志文件的创建时间,精确为 23 秒钟,虽然其晚于 phpinfo.php 开始运行。由此可以确定,Nginx + php-fpm 在 CentOS 7 下,是支持并行请求的。如图9

图9

sleep-0-1585202045.txt
sleep-1-1585202022.txt

10、编辑 Nginx 配置文件,修改 server_name 的值:api.channel-pub.wjdev.chinamcloud.cn localhost,以在一个请求(https://api.channel-pub.wjdev.chinamcloud.cn)中 CURL 另一个请求(http://localhost)(总计请求次数为 7),直接请求本机,以节省网络开销。其总体响应时间长度为:1956ms,2 秒左右,符合预期。如图10

图10

11、直接请求本机,其并发请求数受限于 php-fpm.conf 中的 pm.max_children 值。现在默认设置为:40。子进程的数量固定为 40 个。当在一个请求(https://api.channel-pub.wjdev.chinamcloud.cn)中 CURL 另一个请求(http://localhost)的数量为 52 时,其总体响应时间长度为:6.00s。如图11

图11

12、当请求数量为 42 时,其总体响应时间长度为:4.73s。当请求数量为 35 时,其总体响应时间长度为:4.39s。当请求数量为 30 时,其总体响应时间长度为:3.79s。当请求数量为 25 时,其总体响应时间长度为:2.98s。当请求数量为 20 时,其总体响应时间长度为:2.85s。当请求数量为 15 时,其总体响应时间长度为:2.36s。当请求数量为 10 时,其总体响应时间长度为:2.21s。

13、在服务器上 curl,查看 php-fpm 的状态页。

[root@5b9088596e62 /]# curl http://127.0.0.1/status
pool:                 www
process manager:      static
start time:           26/Mar/2020:13:50:05 +0800
start since:          4603
accepted conn:        516
listen queue:         0
max listen queue:     0
listen queue len:     128
idle processes:       39
active processes:     1
total processes:      40
max active processes: 10
max children reached: 0
slow requests:        0

14、最后分析测试的结果: Windows + Nginx + php-fpm(串行)、CentOS + Nginx + php-fpm(并行)、MacOS + Nginx + php-fpm(并行)、MacOS + Apache(并行)。

永夜