簡體   English   中英

如何在 docker 中使用上游隨機指令運行 nginx 負載均衡器

[英]How to run nginx load balancer with upstream random directive in docker

這是我的docker-compose.yml

version: '3'

services:
  nginx:
    image: nginx:alpine
    restart: always
    hostname: nginx
    container_name: nginx
    ports:
      - 8088:80
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
      - ./index.php:/usr/share/nginx/html/index.php
  php7:
    hostname: php7
    container_name: php7
    image: php:7.4-fpm-alpine
    volumes:
      - ./index.php:/usr/share/nginx/html/index.php
  php8:
    image: php:8.1-fpm-alpine
    hostname: php8
    container_name: php8
    volumes:
      - ./index.php:/usr/share/nginx/html/index.php

這是default.conf

upstream php {
  random two;
  server php7:9000;
  server php8:9000;
}

server {
    listen       80;
    server_name  localhost;

    access_log  /dev/stderr  main;
    error_log /dev/stderr;

    location / {
        root   /usr/share/nginx/html;
        index  index.php info.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        fastcgi_pass php;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
#  location ~ \.php(/|$) {
#    fastcgi_hide_header Content-Type;
#    try_files $uri $uri/ /index.php?$query_string;
#    fastcgi_pass php7:9000;
#    fastcgi_split_path_info ^(.+\.php)(/.*)$;
#    include fastcgi_params;
#    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
#    fastcgi_param DOCUMENT_ROOT $realpath_root;
#  }
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
}

But when I type http://IP:8088/index.php , I get 404 error in web browser, but there's no log in nginx to troubleshoot more.

我想在 nginx 中測試一個簡單的負載均衡器,這樣當我輸入http://IP:8088/index.php時,我看到了一次又一次看到了 74 次。

首先,我這樣做對嗎?

第二,我怎么可能擁有它?

原因是您需要告訴php-fpm location ~ \.php$ {}塊中的某些參數才能找到index.php文件。 對我有用的正確 nginx conf 文件:

upstream php {
  random two;
  server php7:9000;
  server php8:9000;
}

server {
    listen       80;
    server_name  localhost;

    access_log  /dev/stderr  main;
    error_log /dev/stderr;

    root   /usr/share/nginx/html;
    index  index.php info.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    
    location ~ \.php$ {
       fastcgi_pass php;
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

請注意,您需要將root指令移到location /塊之外,以便正確設置$document_root

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM