为什么我的nginx proxy_pass会产生404错误?

时间:2018-03-09 16:33:56

标签: nginx

我试图建立一个docker-compose集群,其中Nginx作为wordpress网站的网关。我的docker-compose.yml如下:

version: '3.3'

services:
  db:
    image: mysql:latest
    volumes:
      - ./mysql/data:/var/lib/mysql
    restart: always
    env_file:
      - ./mysql/mysql.env

  banburritos:
    depends_on:
      - db
    image: wordpress:latest
    restart: always
    env_file:
      - ./wordpress/wordpress.env

  nginx:
    depends_on:
      - banburritos  # maximize chance of nginx starting after everything else
    image: nginx:latest
    ports:
      - "80:80"
    restart: always
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro

wordpress.env文件包含:

WORDPRESS_DB_HOST=db:3306
WORDPRESS_DB_USER=wordpress
WORDPRESS_DB_PASSWORD=somesupersecretpassword

mysql.env文件包含:

MYSQL_ROOT_PASSWORD=anothertopsecretpassword
MYSQL_DATABASE=wordpress
MYSQL_USER=wordpress
MYSQL_PASSWORD=somesupersecretpassword

使用docker-compose up --force-recreate启动群集后,我尝试访问http://localhost/banburritos并获得了404。

在nginx日志中,我看到:

nginx_1        | 172.18.0.1 - - [09/Mar/2018:16:29:29 +0000] "GET /banburritos/ HTTP/1.1" 302 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.106 Safari/537.36"
nginx_1        | 172.18.0.1 - - [09/Mar/2018:16:29:29 +0000] "GET /wp-admin/install.php HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.106 Safari/537.36"
nginx_1        | 2018/03/09 16:29:29 [error] 5#5: *1 open() "/etc/nginx/html/wp-admin/install.php" failed (2: No such file or directory), client: 172.18.0.1, server: localhost, request: "GET /wp-admin/install.php HTTP/1.1", host: "localhost"

我的nginx.conf如下:

worker_processes  8;


events {
    worker_connections  1024;
}


http {
    # Use docker's built-in DNS resolver
    resolver 127.0.0.11 ipv6=off;

    include       mime.types;
    # anything written in /opt/nginx/conf/mime.types is interpreted as if written inside the http { } block

    default_type  application/octet-stream;
    #

    sendfile        on;
    # If serving locally stored static files, sendfile is essential to speed up the server,
    # But if using as reverse proxy one can deactivate it

    #tcp_nopush     on;
    # works opposite to tcp_nodelay. Instead of optimizing delays, it optimizes the amount of data sent at once.

    #keepalive_timeout  0;
    keepalive_timeout  65;
    # timeout during which a keep-alive client connection will stay open.

    #gzip  on;
    # tells the server to use on-the-fly gzip compression.

    server {
        # You would want to make a separate file with its own server block for each virtual domain
        # on your server and then include them.
        listen       80;
        #tells Nginx the hostname and the TCP port where it should listen for HTTP connections.
        # listen 80; is equivalent to listen *:80;

        server_name  localhost monolith.lon.sentimens.com lthibault.sentimens.com;
        # lets you doname-based virtual hosting

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #The location setting lets you configure how nginx responds to requests for resources within the server.
            root   html;
            index  index.html index.htm;
        }

        location /banburritos/ {
            proxy_pass http://banburritos:80$request_uri;

            proxy_read_timeout    90;
            proxy_connect_timeout 90;
            proxy_redirect        off;

            proxy_set_header      X-Real-IP $remote_addr;
            proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header      Host $host;
        }

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

运行`curl -i http://localhost/banburritos/',我得到:

HTTP/1.1 302 Found
Server: nginx/1.13.9
Date: Fri, 09 Mar 2018 15:20:28 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: keep-alive
X-Powered-By: PHP/7.2.3
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Location: http://localhost/wp-admin/install.php

我认为最好,Wordpress(容器名称= banburritos)正在重定向到初始配置页面,但返回的重定向网址并不包含/banburritos/路径段。因此,当浏览器尝试访问http://localhost/wp-admin/install.php时,nginx.conf中没有现有规则,因此我们获得了404。

一个关键的限制是我计划添加额外的wordpress容器,所以我不能做一些事情,例如将\.php$映射到banburritos

的问题:

  1. 我的诊断是否正确?
  2. 解决此问题的推荐方法是什么?

0 个答案:

没有答案