How to change the root path in Nginx configuration - docker

I have created a simple HTML application and I was trying to host it using nginx server.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Simple Test Web App!</title>
</head>
<body >
Test Web App!!!
</body>
</html>
Dockerfile
FROM nginx:latest
# Copy files and directories from the application
COPY index.html /usr/share/nginx/html
# Tell Docker we are going to use this port
EXPOSE 8080
Docker build
docker build -t webapp .
Docker run
docker run -p 8080:8080 webapp:latest
I am able to run the application using http://localhost:8080/ URL. But I want to know how we can change root path to http://localhost:8080/webapp URL.. Means it should serves the request from http://localhost:8080/webapp URI as it was doing with http://localhost:8080/

if you want to redirect your HTML page with subdirectory like HTTP://localhost:8080/webapp instead of HTTP://localhost:8080,
for that, you need to create nginx configuration file that redirect your HTML page into your subdirectory URL. in your nginx configuration file you should add following line,
location /htmlsite { root /var/www/htmlsite; try_files $uri $uri/ =404; }

Related

How to change nginx default homepage to a static HTML website of my own?

I have a very simple HTML called index.html file which generates a website along with some javascript and css.
In the same folder, there's a Dockerfile of my creation with the following lines :
# use nginx:alpine as base image
FROM nginx:alpine
# copy static website index.html into nginx default html folder
COPY index.html /usr/share/nginx/html
# export port 80
EXPOSE 80
# run nginx in the foreground
CMD ["nginx", "-g", "daemon off;"]
The problem is that nginx default homepage doesn't change when I build and run my application :
$ docker build -t some-content-nginx .
$ docker run --name some-nginx -d some-content-nginx
I've tried a ton of things but I just can't seem to get my website to show up at the localhost URL.
Can someone please point me in the right direction?
I spent the last 10 hours trying to solve this and turns out it was a cache issue.
Solved it by clearing the cache for localhost using shift+ F5.

Basic Nginx container (static file)

I am trying to do the simplest possible container with Nginx hosting a simple index.html file. But I can't seem to get it working. I must be missing a concept somewhere and I am hoping for a bit of help.
Here is what I am doing:
I have a folder with 2 files in it:
Dockerfile
FROM nginx:1.18.0
WORKDIR /usr/share/nginx/html
COPY index.html ./
index.html
<!DOCTYPE html>
<html>
<head>
<title>Testing</title>
</head>
<body>
<p>Hello Test App</p>
</body>
</html>
First I build the container. From the folder that has the files, I run this command:
docker image build --tag nginx-test:1.0.0 --file ./Dockerfile .
Then I run the container:
docker run -d -p 3737:3737 nginx-test:1.0.0
Then I browse to http://localhost:3737 (I also tried http://localhost:3737/index.html) and chrome shows an ERR_EMPTY_RESPONSE error:
How can I get my index.html file to be hosted in my container?
The problem was with this command:
docker run -d -p 3737:3737 nginx-test:1.0.0
The -p option is selecting the ports. The first port is what will be exposed on the host machine. The second port is what will be used to communicate with the container. By default, nginx uses port 80 (like most web servers).
So changing that command to:
docker run -d -p 3737:80 nginx-test:1.0.0
Causes the rest of the steps in my scenario to work correctly.

I can't see my html when I built container with docker

I have my container running like this
in the file directory C:\Users\hailey\Desktop\GitTest Where my project file are.
# getting bse image nginx
FROM nginx
MAINTAINER hailey
COPY . /usr/share/nginx/html
This is my docker file and I want to run my html file, which located in C:\Users\hailey\Desktop\GitTest
When I accessed to http://127.0.0.1:8080/
I see only this page, which is not helloWorld.html
you can copy and replace testhelloWorld.html with index.html
COPY testhelloWorld.html /usr/share/nginx/html/index.html

Nginx static webpage and Docker with env variable url

I've got a nginx web server with a webpage inside that makes some http calls.
I want to dockerize all and use a parametric URL.
Is there a way to do this? Is it possible?
If I understood correctly you need a dynamic web page containing a script that makes HTTP calls to a configurable URL.
That can be achieved using Server Side Includes. with nginx. The webpage could include a configuration file that is created during the initialization of the container.
Create the file into the nginx document root when the image is first started. For example:
docker run -e URL=http://stackoverflow.com --entrypoint="/bin/sh" nginx -c 'echo ${URL} > /usr/share/nginx/html/url_config & exec nginx -g "daemon off;"'
For a real world scenario create a custom image based on nginx and override the entrypoint. The entrypoint creates the configuration file with the URL environment variable and eventually launches nginx in foreground.
Include the configuration file in the web page using the #include SSI directive
<script>
...
const http = new XMLHttpRequest();
const url = '<!--#include file="url_config" -->';
http.open("GET", url);
http.send();
...
Configure nginx to process SSI by adding the ssi on; directive
http {
ssi on;
...
}
Hope it helps.
Yes, it is possible,But you can do this using docker network if you want some routing. but you can do anything with Docker. According you question title you want to use ENV in URL in the Nginx Configuration. Here is the way to do that.
Set Environment in Docker run command or Dockerfile
Use that Environment variable in your Nginx config file
docker run --name nginx -e APP_HOST_NAME="myexample.com" -e APP_HOST_PORT="3000" yourimage_nginx
Now you can use these URL in your nginx configuration.
server {
set_by_lua $curr_server_name 'return os.getenv("APP_HOST_NAME")';
set_by_lua $curr_server_port 'return os.getenv("APP_HOST_PORT")';
proxy_pass http://$curr_server_name/index.html;
}
To deal with ENV with nginx you can check lua-nginx-module

How to setup mass dynamic virtual hosts in nginx on docker?

How can I setup mass dynamic virtual hosts in nginx As seen here
except using docker as the host machine?
I currently have it setup like this:
# default.conf
server {
root /var/www/html/$http_host;
server_name $http_host;
}
And in my Dockerfile
COPY default.conf /etc/nginx/sites-enabled/default.conf
And after I build the image and run it:
docker run -d 80:80 -v www/:/var/www/html
But when I point a new domain (example.dev) in my hosts file and make a www/example.dev/index.html. It doesn't work at all.
The setup is correct and it works as i tested on my system. The only issue is that you are copying the file on a wrong path. The docker image doesn't use the sites-enabled path by default. The default config loads everything present in /etc/nginx/conf.d. So you need to copy to that path and rest all works great
COPY default.conf /etc/nginx/conf.d/default.conf
Make sure to map you volumes correctly. While testing I tested it using below docker command
docker run -p 80:80 -v $PWD/www/:/var/www/html -v $PWD/default.conf:/etc/nginx/conf.d/default.conf nginx
Below is the output on command line
vagrant#vagrant:~/test/www$ mkdir dev.tarunlalwani.com
vagrant#vagrant:~/test/www$ cd dev.tarunlalwani.com/
vagrant#vagrant:~/test/www/dev.tarunlalwani.com$ vim index.html
vagrant#vagrant:~/test/www/dev.tarunlalwani.com$ cat index.html
<h1>This is a test</h1>
Output on browser

Resources