简体   繁体   English

使用自定义vhost在docker中运行nginx

[英]Run nginx in docker with custom vhost

I want to run basic nginx container on OSX with docker machine. 我想在带有docker机器的OSX上运行基本的nginx容器。

I run the following code: 我运行以下代码:

docker pull nginx
docker run -d -i -t \
    -p 8080:80 \
    -v $(pwd)/vhost.conf:/etc/nginx/sites-enabled/vhost.conf \
    -v $(pwd)/docker.dev:/var/www/docker.dev nginx

In current directory I have vhost.conf file: 在当前目录中,我有vhost.conf文件:

server {
    listen 80;
    index index.html;
    server_name docker.dev;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log
    root /var/www/docker.dev
}

and docker.dev folder with index.html file in it. 和其中带有index.html文件的docker.dev文件夹。

Also in my /etc/hosts on my host OSX system I have: 同样在主机OSX系统的/ etc / hosts中,我还有:

192.168.99.100 docker.dev

But when I visit http://192.168.99.100:8080 in browser I get default nginx page, not my index.html 但是,当我在浏览器中访问http://192.168.99.100:8080时 ,会得到默认的nginx页面,而不是我的index.html

I can achieve the desired result by placing index.html file in /usr/share/nginx/html folder. 我可以通过将index.html文件放在/ usr / share / nginx / html文件夹中来获得所需的结果。 But I want to figure out how can I adjust my own location for web root. 但是我想弄清楚如何调整自己的Web根目录位置。

Incorrect location for nginx.conf nginx.conf的位置不正确

You're using an incorrect location for the configuration file. 您为配置文件使用了错误的位置。 As described on the readme for the official nginx image , the location for the configuration file is /etc/nginx/nginx.conf , so you should use; 官方nginx映像自述文件所述,配置文件的位置为/etc/nginx/nginx.conf ,因此应使用;

-v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro

For the configuration file. 对于配置文件。

Given that you'd be serving only a single website in your container, you don't even have to specify the domainname in your virtual host (any request that arrives at the nginx container should be for that domain). 假设您只在容器中提供一个网站,您甚至不必在虚拟主机中指定域名(到达nginx容器的任何请求都应针对该域)。

Serving multiple domains 服务多个域

In general, I'd recommend using a dedicated container for each domain/website you want to run. 通常,我建议为要运行的每个域/网站使用专用的容器。 To serve multiple websites (thus, multiple containers) on a single docker machine, you can either; 要在单个docker机器上服务多个网站(因此,多个容器),您可以:

Use port-mapping; 使用端口映射; each container is published on a different port in the host ( -p 8080:80 for website "A", -p 8081:80 for website "B") 每个容器都发布在主机的其他端口上(网站“ A”为-p 8080:80 -p 8081:80 ,网站“ B”为-p 8081:80

Use a proxy container. 使用代理容器。 This is probably a nicer approach; 这可能是更好的方法。 You run your website containers as usual, but don't publish their ports. 您像往常一样运行网站容器,但发布其端口。 Those containers are thus not publicly reachable. 因此,这些容器是无法公开获得的。 Instead of publishing the ports directly, you run a proxy-server that forwards traffic to the right container(s), based on the domain-name. 您可以运行代理服务器,而不是直接发布端口,而是根据域名将流量转发到正确的容器。

Domainname-based forwarding requires some configuration. 基于域名的转发需要一些配置。 However, there's a convenient way to do this fully automatic. 但是,有一种方便的方法可以完全自动执行此操作。 jwilder/nginx-proxy is a nginx proxy-server that's designed to run in a Docker container; jwilder / nginx-proxy是nginx代理服务器,旨在在Docker容器中运行; all you need to do, is run that container, and give it access to the docker API. 您所需要做的就是运行该容器,并为其提供对Docker API的访问权限。

Subsequently, start your webserver containers, but set an environment variable on them that specifies which domains they serve (for example -e VIRTUAL_HOST=www.example.com,example.com ). 随后,启动您的Web服务器容器,但是在它们上设置一个环境变量,以指定它们服务的域(例如-e VIRTUAL_HOST=www.example.com,example.com )。

The nginx-proxy container listens for "docker events", and automatically generates the configuration to forward traffic to the right container. nginx-proxy容器侦听“ docker事件”,并自动生成配置以将流量转发到正确的容器。

If your DNS (or /etc/hosts ) is correctly set up, you can now simply visit http://www.example.com , and view that website. 如果您的DNS(或/etc/hosts )设置正确,则现在只需访问http://www.example.com并查看该网站。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM