docker安装nginx,关于配置.conf的问题

我尝试使用docker来搭建环境lnmp,把nginx和php-fpm都搭建好了。现在需要通过域名访问测试一下;
nginx的项目目录是通过映射访问的,在虚拟机的/docker-contain目录下
我创建了这几个目录
www logs conf conf.d
然后我把容器中的nginx.conf配置文件复制了一份到 /docker-contain/conf/ 目录下

然后又在/docker-contain/conf.d/目录下创建了站点配置文件www.dockertest.com.conf

配置内容:

server {
    listen  80;
    server_name  www.dockertest.com;

    root   /usr/share/nginx/html/test;

    location / {
        index  index.php index.html index.htm;

        if ( !-e $request_filename) {
            rewrite ^(.*)$ /index.php?s=$1 last;
            break;
        }
        proxy_set_header  Host  $http_host;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass  http://192.168.72.158:80;

    }
   
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html/test;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php(.*)$ {
            root          /var/www/html/test;
            fastcgi_pass   172.17.0.3:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO        $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
     }

windows上通过域名访问 www.dockertest.com 总是报502 查看日志

6#6: *1030 upstream prematurely closed connection while reading response header from upstream, client: 172.17.0.1, server: www.dockertest.com, request: "GET / HTTP/1.0", upstream: "http://192.168.72.158:80/", host: "www.dockertest.com"

疑问:
1.上面配置时index 我已经指定了首页索引文件为什么直接通过域名不能定位到?

2.但是通过 www.dockertest.com/**.php 在后面加上php文件 可以正常访问test目录下的php文件;

但是 访问www.dockertest.com/html.html 时又报502错误。

阅读 2.5k
1 个回答

如果你确认代理到 http://192.168.72.158:80/是没有错的, 那就是你的后端出了问题,查看一下http://192.168.72.158:80/服务对应的出错日志.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题