Nginx代理和Docker不能与Laravel合作

时间:2017-10-18 18:09:39

标签: laravel docker jwilder-nginx-proxy

我在Laravel有一个网站,在Node.js有一个API。我想将网站托管到domain.tld,将API托管到api.domain.tld。首先,我为Laravel项目制作了一个Dockerfile:

FROM shakyshane/laravel-php:latest

COPY composer.lock composer.json /var/www/my_site

COPY database /var/www/my_site/database

WORKDIR /var/www/my_site

COPY . /var/www/my_site

RUN php composer.phar install --no-dev --no-scripts \
    && rm composer.phar

RUN chown -R www-data:www-data \
        /var/www/my_site/storage \
        /var/www/my_site/bootstrap/cache

RUN php artisan optimize

EXPOSE 80 443 9000

VOLUME [ "/var/www/my_site" ]

关注this tutorial后,我开始nginx-proxy这样的图像

docker run -d -p 80:80 -p 443:443 -v /var/run/docker.sock:/tmp/docker.sock -v /home/my_user/docker_proxy.conf:/etc/nginx/conf.d/my_proxy.conf:ro -v /etc/ssl/certs/dhparam.pem:/etc/ssl/certs/dhparam.pem -v /etc/letsencrypt:/etc/letsencrypt:rw -v /etc/nginx/snippets/fastcgi-php.conf:/etc/nginx/snippets/fastcgi-php.conf -v /etc/nginx/fastcgi.conf:/etc/nginx/fastcgi.conf --name proxy jwilder/nginx-proxy

启动我的网站容器:

docker container run -d --expose 80 --expose 443 -e VIRTUAL_HOST=domain.tld -e VIRTUAL_PORT=80,443 -e VIRTUAL_PROTO=https -v /var/www/storage:/var/www/storage --name my_site my_site

根据教程,这应该有用,但它没有。我还试图为代理设置我的自定义配置文件,它的服务器类似于/etc/nginx/sites-available/domain.tld中的配置文件

docker_proxy.conf文件:

# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  default $http_x_forwarded_proto;
  ''      $scheme;
}
# If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
# server port the client connected to
map $http_x_forwarded_port $proxy_x_forwarded_port {
  default $http_x_forwarded_port;
  ''      $server_port;
}
# If we receive Upgrade, set Connection to "upgrade"; otherwise, delete any
# Connection header that may have been passed to this server
map $http_upgrade $proxy_connection {
  default upgrade;
  '' close;
}
# Apply fix for very long server names
server_names_hash_bucket_size 128;
# Default dhparam
ssl_dhparam /etc/ssl/certs/dhparam.pem;
# Set appropriate X-Forwarded-Ssl header
map $scheme $proxy_x_forwarded_ssl {
  default off;
  https on;
}
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
log_format vhost '$host $remote_addr - $remote_user [$time_local] '
                 '"$request" $status $body_bytes_sent '
                 '"$http_referer" "$http_user_agent"';
access_log off;
resolver 168.63.129.16;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
# Mitigate httpoxy attack (see README for details)
proxy_set_header Proxy "";
server {
    server_name _; # This is just an invalid value which will never trigger on a real hostname.
    listen 80;
    access_log /var/log/nginx/access.log vhost;
    return 503;
}
# domain.tld
upstream domain.tld {
                ## Can be connect with "bridge" network
            # my_site
            server 172.17.0.2:80;
}
server {
    server_name www.domain.tld;
    server_name domain.tld;
    listen 80 ;
    access_log /var/log/nginx/access.log vhost;
    rewrite ^(.*) https://$host$request_uri$1 permanent;
}
server {
    server_name domain.tld;
    server_name www.domain.tld;
    listen 443 ssl http2;

    access_log /var/log/nginx/access.log vhost;

    index index.php index.html index.htm index.nginx-debian.html;

    ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.tld/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:ECDHE-RSA-AES128-GCM-SHA256:AES256+EECDH:DHE-RSA-AES128-GCM-SHA256:AES256+EDH:ECDHE-$";
    ssl_session_cache shared:SSL:10m;

    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl on;
    ssl_session_timeout 1d;
    ssl_session_tickets off;
    ssl_stapling on;
    ssl_stapling_verify on;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        proxy_pass http://domain.tld;
    }
}

但它也没有用。这会引发404,我相信这种情况正在发生,因为我没有将root属性设置为配置文件,但是当我将网站的卷设置为nginx-proxy容器时,将其设置为root I& #39; m收到502并出现以下错误消息:

[error] 34#34: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 94.67.147.44, server: domain.tld, request: "GET / HTTP/2.0", upstream: "http://172.17.0.2:80/", host: "domain.tld"

我做错了什么?

感谢。

0 个答案:

没有答案