403禁止在Docker中使用Nginx容器

时间:2017-12-11 21:47:44

标签: php docker nginx

我正在尝试使用我的nginx服务器呈现静态文件。我的静态文件 - index.html和login.html分别位于public / templates / index.html和public / templates / login.html中。问题是我每次都得到403禁止错误,我运行我的localhost服务器。如果我将静态文件(index.html和login.html)从templates目录移动到公共目录,它将完美呈现。我使用docker来管理nginx和localhost服务器。

DockerFile

FROM php:7
RUN pecl install ev-beta && \
   docker-php-ext-enable ev
WORKDIR /opt/app
CMD ["/usr/local/bin/php", "server.php"] 

Nginx服务器配置文件(nginx.conf)

#user  nobody;
worker_processes  1;

error_log /usr/local/var/log/nginx/nginx_error.log warn;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;

events {
   worker_connections  1024;
}

map $http_upgrade $connection_upgrade{
    default upgrade;
    '' close;
}

server {
    location / {
root /usr/share/nginx/html;
    #root /var/www;
    try_files $uri $uri/index.html @phpsite;
}

location @phpsite {
    proxy_http_version 1.1;
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_pass http://app:8080;
}
}

Server.php

require_once 'vendor/autoload.php';
require_once 'src/SlimAdapterServer.php';
require_once 'src/SessionProvider.php';

use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;

$provider = new \Authentication\SessionProvider();
$slim = new \Slim\App();

//HomePage Route;
$slim->get('/', function(Request $req, Response $res) use ($provider): Response{

$sessionId = $req->getCookieParams()['session'] ?? '';

if(!$provider->hasSession($sessionId)){
  return $res->withRedirect('/login');
}

$res->getBody()->write(file_get_contents('templates/index.html'));
return $res->withHeader('Content-Type', 'text/html;charset=utf8');
});

//Login Route
$slim->get('/login', function(Request $req, Response $res): Response {
$res->getBody()->write(file_get_contents('templates/login.html'));
return $res->withHeader('Content-Type', 'text/html;charset=utf8');
});

$adapter = new \Http\SlimAdapterServer($slim);

$app = new \Ratchet\App('localhost', 8080, '0.0.0.0');
$app->route('/', $adapter, ['*']);
$app->route('/login', $adapter, ['*']);
$app->route('/authenticate', $adapter, ['*']);
$app->run();

我希望我的php脚本能够检测用户是否已登录,如果是,则重定向到index.html页面,否则重定向到login.html页面。

从Docker Webserver容器中记录错误

172.17.0.1 - - [11/Dec/2017:20:44:02 +0000] "GET / HTTP/1.1" 403 169 "-" "curl/7.54.0" "-" 2017/12/11 20:44:02 [error] 6#6: *8 directory index of "/usr/share/nginx/html/" is forbidden, client: 172.17.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"

0 个答案:

没有答案