Is it necessary to specify VIRTUAL_HOST when using an nginx proxy (with docker containers)?
I've been looking at a lot of tutorials on docker containers, microservices and nginx proxy. One thing that really caught my attention is that in the docker-compose.yml file, the service that I'd want to start up has a VIRTUAL_HOST environment variable. That way I can start the container by directly referencing the VIRTUAL_HOST.
But do all the containers need to have VIRTUAL_HOST specified? For example, suppose I have 3 containers: A, B and C. If I want all 3 of these containers to be proxied, then do I have to specify a VIRTUAL_HOST environment variable for all 3 of them in the docker-compose file? Is the VIRTUAL_HOST environment variable a prerequisite for Nginx proxy to work properly with these containers? None of the tutorials I checked answers this question.
docker nginx
add a comment |
I've been looking at a lot of tutorials on docker containers, microservices and nginx proxy. One thing that really caught my attention is that in the docker-compose.yml file, the service that I'd want to start up has a VIRTUAL_HOST environment variable. That way I can start the container by directly referencing the VIRTUAL_HOST.
But do all the containers need to have VIRTUAL_HOST specified? For example, suppose I have 3 containers: A, B and C. If I want all 3 of these containers to be proxied, then do I have to specify a VIRTUAL_HOST environment variable for all 3 of them in the docker-compose file? Is the VIRTUAL_HOST environment variable a prerequisite for Nginx proxy to work properly with these containers? None of the tutorials I checked answers this question.
docker nginx
add a comment |
I've been looking at a lot of tutorials on docker containers, microservices and nginx proxy. One thing that really caught my attention is that in the docker-compose.yml file, the service that I'd want to start up has a VIRTUAL_HOST environment variable. That way I can start the container by directly referencing the VIRTUAL_HOST.
But do all the containers need to have VIRTUAL_HOST specified? For example, suppose I have 3 containers: A, B and C. If I want all 3 of these containers to be proxied, then do I have to specify a VIRTUAL_HOST environment variable for all 3 of them in the docker-compose file? Is the VIRTUAL_HOST environment variable a prerequisite for Nginx proxy to work properly with these containers? None of the tutorials I checked answers this question.
docker nginx
I've been looking at a lot of tutorials on docker containers, microservices and nginx proxy. One thing that really caught my attention is that in the docker-compose.yml file, the service that I'd want to start up has a VIRTUAL_HOST environment variable. That way I can start the container by directly referencing the VIRTUAL_HOST.
But do all the containers need to have VIRTUAL_HOST specified? For example, suppose I have 3 containers: A, B and C. If I want all 3 of these containers to be proxied, then do I have to specify a VIRTUAL_HOST environment variable for all 3 of them in the docker-compose file? Is the VIRTUAL_HOST environment variable a prerequisite for Nginx proxy to work properly with these containers? None of the tutorials I checked answers this question.
docker nginx
docker nginx
asked Dec 11 at 20:29
Vktr
111
111
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I assume you're talking about the jwilder/nginx-proxy image.
That way I can start the container by directly referencing the VIRTUAL_HOST.
The container is started as soon as you type docker-compose up
and it's running all the time. VIRTUAL_HOST
tells the reverse proxy which host requests should be forwarded to this container.
For example if you have a container with VIRTUAL_HOST=foo.example.com
, then each request made to the revproxy with foo.example.com
in the Host
header will be forwarded to this container and its response will be sent by the proxy to client. But if the Host
header is bar.example.com
, the request will not be forwarded to that container. Instead, proxy will forward it to a container with VIRTUAL_HOST=bar.example.com
. If no such container exists, it will try to use the default host specified with DEFAULT_HOST
variable on the proxy container (not default container). If default host isn't configured, it will return an error.
Without VIRTUAL_HOST
, the proxy won't know which Host
this container corresponds to, so no requests will be forwarded.
But aren't there any other ways for the proxy to identify which container the request should be forwarded to? Perhaps by using the IP address of the container...or something like that?
– Vktr
Dec 11 at 21:32
@Vktr I think you're misunderstanding the issue. The purpose of this reverse proxy is to distribute requests across multiple HTTP servers based on request data, specifically theHost
header user's browser sends (which contains website's URL up to the first slash). It needs to somehow know whichHost
header corresponds to which container. The browser doesn't know that the revproxy is there, it also doesn't know that you're running the server in Docker or what container IPs are. That's what this revproxy is for, it hides complexity of your infrastructure. Does that make it any clearer?
– gronostaj
Dec 11 at 21:49
Ok that makes sense. I totally forgot that the host header is a part of the http request. But what doesn't make sense is this: In many of the tutorials I checked online, the VIRTUAL_HOST is only specified for one service in the docker-compose file. In all the other services, no VIRTUAL_HOST is specified. Why is that? Here is an example of such a tutorial: Developing with Docker. Check part 4. The VIRTUAL_HOST is only specified for one service, despite the fact that this service has links to the other services. Why is that?
– Vktr
Dec 12 at 9:52
Other services aren't user-facing and accessible through HTTP.
– gronostaj
Dec 12 at 11:27
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1382756%2fis-it-necessary-to-specify-virtual-host-when-using-an-nginx-proxy-with-docker-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I assume you're talking about the jwilder/nginx-proxy image.
That way I can start the container by directly referencing the VIRTUAL_HOST.
The container is started as soon as you type docker-compose up
and it's running all the time. VIRTUAL_HOST
tells the reverse proxy which host requests should be forwarded to this container.
For example if you have a container with VIRTUAL_HOST=foo.example.com
, then each request made to the revproxy with foo.example.com
in the Host
header will be forwarded to this container and its response will be sent by the proxy to client. But if the Host
header is bar.example.com
, the request will not be forwarded to that container. Instead, proxy will forward it to a container with VIRTUAL_HOST=bar.example.com
. If no such container exists, it will try to use the default host specified with DEFAULT_HOST
variable on the proxy container (not default container). If default host isn't configured, it will return an error.
Without VIRTUAL_HOST
, the proxy won't know which Host
this container corresponds to, so no requests will be forwarded.
But aren't there any other ways for the proxy to identify which container the request should be forwarded to? Perhaps by using the IP address of the container...or something like that?
– Vktr
Dec 11 at 21:32
@Vktr I think you're misunderstanding the issue. The purpose of this reverse proxy is to distribute requests across multiple HTTP servers based on request data, specifically theHost
header user's browser sends (which contains website's URL up to the first slash). It needs to somehow know whichHost
header corresponds to which container. The browser doesn't know that the revproxy is there, it also doesn't know that you're running the server in Docker or what container IPs are. That's what this revproxy is for, it hides complexity of your infrastructure. Does that make it any clearer?
– gronostaj
Dec 11 at 21:49
Ok that makes sense. I totally forgot that the host header is a part of the http request. But what doesn't make sense is this: In many of the tutorials I checked online, the VIRTUAL_HOST is only specified for one service in the docker-compose file. In all the other services, no VIRTUAL_HOST is specified. Why is that? Here is an example of such a tutorial: Developing with Docker. Check part 4. The VIRTUAL_HOST is only specified for one service, despite the fact that this service has links to the other services. Why is that?
– Vktr
Dec 12 at 9:52
Other services aren't user-facing and accessible through HTTP.
– gronostaj
Dec 12 at 11:27
add a comment |
I assume you're talking about the jwilder/nginx-proxy image.
That way I can start the container by directly referencing the VIRTUAL_HOST.
The container is started as soon as you type docker-compose up
and it's running all the time. VIRTUAL_HOST
tells the reverse proxy which host requests should be forwarded to this container.
For example if you have a container with VIRTUAL_HOST=foo.example.com
, then each request made to the revproxy with foo.example.com
in the Host
header will be forwarded to this container and its response will be sent by the proxy to client. But if the Host
header is bar.example.com
, the request will not be forwarded to that container. Instead, proxy will forward it to a container with VIRTUAL_HOST=bar.example.com
. If no such container exists, it will try to use the default host specified with DEFAULT_HOST
variable on the proxy container (not default container). If default host isn't configured, it will return an error.
Without VIRTUAL_HOST
, the proxy won't know which Host
this container corresponds to, so no requests will be forwarded.
But aren't there any other ways for the proxy to identify which container the request should be forwarded to? Perhaps by using the IP address of the container...or something like that?
– Vktr
Dec 11 at 21:32
@Vktr I think you're misunderstanding the issue. The purpose of this reverse proxy is to distribute requests across multiple HTTP servers based on request data, specifically theHost
header user's browser sends (which contains website's URL up to the first slash). It needs to somehow know whichHost
header corresponds to which container. The browser doesn't know that the revproxy is there, it also doesn't know that you're running the server in Docker or what container IPs are. That's what this revproxy is for, it hides complexity of your infrastructure. Does that make it any clearer?
– gronostaj
Dec 11 at 21:49
Ok that makes sense. I totally forgot that the host header is a part of the http request. But what doesn't make sense is this: In many of the tutorials I checked online, the VIRTUAL_HOST is only specified for one service in the docker-compose file. In all the other services, no VIRTUAL_HOST is specified. Why is that? Here is an example of such a tutorial: Developing with Docker. Check part 4. The VIRTUAL_HOST is only specified for one service, despite the fact that this service has links to the other services. Why is that?
– Vktr
Dec 12 at 9:52
Other services aren't user-facing and accessible through HTTP.
– gronostaj
Dec 12 at 11:27
add a comment |
I assume you're talking about the jwilder/nginx-proxy image.
That way I can start the container by directly referencing the VIRTUAL_HOST.
The container is started as soon as you type docker-compose up
and it's running all the time. VIRTUAL_HOST
tells the reverse proxy which host requests should be forwarded to this container.
For example if you have a container with VIRTUAL_HOST=foo.example.com
, then each request made to the revproxy with foo.example.com
in the Host
header will be forwarded to this container and its response will be sent by the proxy to client. But if the Host
header is bar.example.com
, the request will not be forwarded to that container. Instead, proxy will forward it to a container with VIRTUAL_HOST=bar.example.com
. If no such container exists, it will try to use the default host specified with DEFAULT_HOST
variable on the proxy container (not default container). If default host isn't configured, it will return an error.
Without VIRTUAL_HOST
, the proxy won't know which Host
this container corresponds to, so no requests will be forwarded.
I assume you're talking about the jwilder/nginx-proxy image.
That way I can start the container by directly referencing the VIRTUAL_HOST.
The container is started as soon as you type docker-compose up
and it's running all the time. VIRTUAL_HOST
tells the reverse proxy which host requests should be forwarded to this container.
For example if you have a container with VIRTUAL_HOST=foo.example.com
, then each request made to the revproxy with foo.example.com
in the Host
header will be forwarded to this container and its response will be sent by the proxy to client. But if the Host
header is bar.example.com
, the request will not be forwarded to that container. Instead, proxy will forward it to a container with VIRTUAL_HOST=bar.example.com
. If no such container exists, it will try to use the default host specified with DEFAULT_HOST
variable on the proxy container (not default container). If default host isn't configured, it will return an error.
Without VIRTUAL_HOST
, the proxy won't know which Host
this container corresponds to, so no requests will be forwarded.
answered Dec 11 at 20:50
gronostaj
27.9k1368107
27.9k1368107
But aren't there any other ways for the proxy to identify which container the request should be forwarded to? Perhaps by using the IP address of the container...or something like that?
– Vktr
Dec 11 at 21:32
@Vktr I think you're misunderstanding the issue. The purpose of this reverse proxy is to distribute requests across multiple HTTP servers based on request data, specifically theHost
header user's browser sends (which contains website's URL up to the first slash). It needs to somehow know whichHost
header corresponds to which container. The browser doesn't know that the revproxy is there, it also doesn't know that you're running the server in Docker or what container IPs are. That's what this revproxy is for, it hides complexity of your infrastructure. Does that make it any clearer?
– gronostaj
Dec 11 at 21:49
Ok that makes sense. I totally forgot that the host header is a part of the http request. But what doesn't make sense is this: In many of the tutorials I checked online, the VIRTUAL_HOST is only specified for one service in the docker-compose file. In all the other services, no VIRTUAL_HOST is specified. Why is that? Here is an example of such a tutorial: Developing with Docker. Check part 4. The VIRTUAL_HOST is only specified for one service, despite the fact that this service has links to the other services. Why is that?
– Vktr
Dec 12 at 9:52
Other services aren't user-facing and accessible through HTTP.
– gronostaj
Dec 12 at 11:27
add a comment |
But aren't there any other ways for the proxy to identify which container the request should be forwarded to? Perhaps by using the IP address of the container...or something like that?
– Vktr
Dec 11 at 21:32
@Vktr I think you're misunderstanding the issue. The purpose of this reverse proxy is to distribute requests across multiple HTTP servers based on request data, specifically theHost
header user's browser sends (which contains website's URL up to the first slash). It needs to somehow know whichHost
header corresponds to which container. The browser doesn't know that the revproxy is there, it also doesn't know that you're running the server in Docker or what container IPs are. That's what this revproxy is for, it hides complexity of your infrastructure. Does that make it any clearer?
– gronostaj
Dec 11 at 21:49
Ok that makes sense. I totally forgot that the host header is a part of the http request. But what doesn't make sense is this: In many of the tutorials I checked online, the VIRTUAL_HOST is only specified for one service in the docker-compose file. In all the other services, no VIRTUAL_HOST is specified. Why is that? Here is an example of such a tutorial: Developing with Docker. Check part 4. The VIRTUAL_HOST is only specified for one service, despite the fact that this service has links to the other services. Why is that?
– Vktr
Dec 12 at 9:52
Other services aren't user-facing and accessible through HTTP.
– gronostaj
Dec 12 at 11:27
But aren't there any other ways for the proxy to identify which container the request should be forwarded to? Perhaps by using the IP address of the container...or something like that?
– Vktr
Dec 11 at 21:32
But aren't there any other ways for the proxy to identify which container the request should be forwarded to? Perhaps by using the IP address of the container...or something like that?
– Vktr
Dec 11 at 21:32
@Vktr I think you're misunderstanding the issue. The purpose of this reverse proxy is to distribute requests across multiple HTTP servers based on request data, specifically the
Host
header user's browser sends (which contains website's URL up to the first slash). It needs to somehow know which Host
header corresponds to which container. The browser doesn't know that the revproxy is there, it also doesn't know that you're running the server in Docker or what container IPs are. That's what this revproxy is for, it hides complexity of your infrastructure. Does that make it any clearer?– gronostaj
Dec 11 at 21:49
@Vktr I think you're misunderstanding the issue. The purpose of this reverse proxy is to distribute requests across multiple HTTP servers based on request data, specifically the
Host
header user's browser sends (which contains website's URL up to the first slash). It needs to somehow know which Host
header corresponds to which container. The browser doesn't know that the revproxy is there, it also doesn't know that you're running the server in Docker or what container IPs are. That's what this revproxy is for, it hides complexity of your infrastructure. Does that make it any clearer?– gronostaj
Dec 11 at 21:49
Ok that makes sense. I totally forgot that the host header is a part of the http request. But what doesn't make sense is this: In many of the tutorials I checked online, the VIRTUAL_HOST is only specified for one service in the docker-compose file. In all the other services, no VIRTUAL_HOST is specified. Why is that? Here is an example of such a tutorial: Developing with Docker. Check part 4. The VIRTUAL_HOST is only specified for one service, despite the fact that this service has links to the other services. Why is that?
– Vktr
Dec 12 at 9:52
Ok that makes sense. I totally forgot that the host header is a part of the http request. But what doesn't make sense is this: In many of the tutorials I checked online, the VIRTUAL_HOST is only specified for one service in the docker-compose file. In all the other services, no VIRTUAL_HOST is specified. Why is that? Here is an example of such a tutorial: Developing with Docker. Check part 4. The VIRTUAL_HOST is only specified for one service, despite the fact that this service has links to the other services. Why is that?
– Vktr
Dec 12 at 9:52
Other services aren't user-facing and accessible through HTTP.
– gronostaj
Dec 12 at 11:27
Other services aren't user-facing and accessible through HTTP.
– gronostaj
Dec 12 at 11:27
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1382756%2fis-it-necessary-to-specify-virtual-host-when-using-an-nginx-proxy-with-docker-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown