{"id":17274158,"url":"https://github.com/davidthorn/nginx","last_synced_at":"2026-01-06T02:51:52.971Z","repository":{"id":122657923,"uuid":"121122735","full_name":"davidthorn/nginx","owner":"davidthorn","description":null,"archived":false,"fork":false,"pushed_at":"2018-02-11T16:11:46.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-31T14:47:46.346Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Makefile","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davidthorn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-11T12:47:46.000Z","updated_at":"2018-02-11T16:11:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"579a7075-b4db-490c-a3f9-8fc3795562d9","html_url":"https://github.com/davidthorn/nginx","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidthorn%2Fnginx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidthorn%2Fnginx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidthorn%2Fnginx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidthorn%2Fnginx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidthorn","download_url":"https://codeload.github.com/davidthorn/nginx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245661472,"owners_count":20651870,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T08:53:15.322Z","updated_at":"2026-01-06T02:51:47.940Z","avatar_url":"https://github.com/davidthorn.png","language":"Makefile","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nginx and docker configuration\n\nThis project is about how to best congifure ngix in docker.\n\n## Installation\n\nThe first thing which I am going to do is to pull the latest nginx image so that this does not have to be done again.\n\n\u003e It seems that it was already on my laptop.\n\n```bash\n$ sudo docker pull nginx\nUsing default tag: latest\nlatest: Pulling from library/nginx\ne7bb522d92ff: Already exists \n6edc05228666: Pull complete \ncd866a17e81f: Pull complete \nDigest: sha256:285b49d42c703fdf257d1e2422765c4ba9d3e37768d6ea83d7fe2043dad6e63d\nStatus: Downloaded newer image for nginx:latest\n```\n\nNow to check that the image is available to be used\n\u003e I have already pull the alpine and php images\n\n```bash\n$ sudo docker image ls\n\nREPOSITORY          TAG                 IMAGE ID            CREATED             SIZE\nphp                 latest              14aa3556d5d5        4 days ago          353MB\nalpine              latest              3fd9065eaf02        4 weeks ago         4.15MB\nnginx               latest              3f8a4339aadd        6 weeks ago         108MB\n```\n\n# Create a dockerfile\n\nMake a directory called `service` and then create a Dockerfile in this folder.\n\n```bash\n$ mkdir -p service\n$ cd service\n$ touch Dockerfile\n```\n\nAdd the following instruction to your Dockerfile. So as to create an image based upon the nginx image\n\n\u003e This example is very redundant, but for the sake of learning lets do it.\n\n```Dockerfile\nFROM nginx\n```\n\nBuild and run our new image.\n\n```bash\n$ sudo docker build -t webserver .\n\nSending build context to Docker daemon  4.608kB\nStep 1/1 : FROM nginx\n ---\u003e 3f8a4339aadd\nSuccessfully built 3f8a4339aadd\nSuccessfully tagged webserver:latest\n```\n\nOnce this has been built we can go ahead and run it to see that our test nginx web server is running and working.\n\n```bash\n# Here we are running the container as a deamon with the flag -d\n# exposing port 80 on the image and binding port 8080 to port 80 on the container\n# --name is giving our container a name of webserver\n# and webserver is telling run which image to use\n$ sudo docker run -d -p 8080:80 --name webserver webserver\n351f6553d438fc0d0f60321b980446613012fad77c18f9509c058c61e974423d\n```\n\nIf you open [http://localhost:8080](http://localhost:8080) in your browser and your should see a 'Welcome to nginx' webpage.\n\nTo check that the container is running we use the ps command\n\n```bash\n$ sudo docker ps\nCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES\n351f6553d438        webserver           \"nginx -g 'daemon ...\"   6 minutes ago       Up 6 minutes        0.0.0.0:8080-\u003e80/tcp   webserver\n```\n\n# Looking inside the nginx container\n\nNow that we can start the container, lets have a look inside it to checkout the conf file.\n\nThe config file which we are wanting to look at is located here: `/etc/nginx/nginx.conf`\n\n```bash\n$ sudo docker exec -it webserver bash\nroot@351f6553d438:/# cat /etc/nginx/nginx.conf\n\nuser  nginx;\nworker_processes  1;\n\nerror_log  /var/log/nginx/error.log warn;\npid        /var/run/nginx.pid;\n\n\nevents {\n    worker_connections  1024;\n}\n\n\nhttp {\n    include       /etc/nginx/mime.types;\n    default_type  application/octet-stream;\n\n    log_format  main  '$remote_addr - $remote_user [$time_local] \"$request\" '\n                      '$status $body_bytes_sent \"$http_referer\" '\n                      '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n\n    access_log  /var/log/nginx/access.log  main;\n\n    sendfile        on;\n    #tcp_nopush     on;\n\n    keepalive_timeout  65;\n\n    #gzip  on;\n\n    include /etc/nginx/conf.d/*.conf;\n}\n\n```\n\nThere are many things in this nginx.conf which we could remove, therefore lets reduce it down to the bare minimum to make this server work.\n\n### nginx.conf minimum requirements\n\n```config\nevents {\n\n}\n\nhttp {\n    server {\n        listen 8888;\n\n        server_name localhost;\n\n        location / {\n            root /usr/share/nginx/html;\n            index index.html;\n        }\n    }\n}\n\n```\n\n### events context\n\n\u003e Reference: http://nginx.org/en/docs/ngx_core_module.html#events\n\n```\nSyntax:\tevents { ... }\nDefault:\t—\nContext:\tmain\nProvides the configuration file context in which the directives \nthat affect connection processing are specified.\n```\n\nIt seems that we must define the events context within side of this config file. Otherwise the server will not function.\n\nI am not sure about anything else about this, but important to note is that it is a requirement for your web server to work!\n\n### http context \n\n\u003e http://nginx.org/en/docs/http/ngx_http_core_module.html#http\n\n```\nSyntax:\thttp { ... }\nDefault:\t—\nContext:\tmain\nProvides the configuration file context in which the HTTP server directives are specified.\n```\n\nThe http block { ... } must sit within side of the nginx.conf because it is a core module and all core modules must be defined within side of the main context\n\nThe http context has many of its own contexts which can be defined within side of its own block such as the server { ... } context\n\n### server context\n\n\u003e http://nginx.org/en/docs/http/ngx_http_core_module.html#server\n\n```\nSyntax:\tserver { ... }\nDefault:\t—\nContext:\thttp\nSets configuration for a virtual server. There is no clear separation between \nIP-based (based on the IP address) and name-based (based on the “Host” request\nheader field) virtual servers. Instead, the listen directives describe all \naddresses and ports that should accept connections for the server, and the \nserver_name directive lists all server names. Example configurations are provided\nin the “How nginx processes a request” document.\n```\n\n\n## Simplify the config file\n\nA simple version of the nginx file could look like this\n\n```conf\n\nevents {\n    # and event contexts go here\n}\n\nhttp {\n\n    server {\n    \n        listen 8888; # port which we want to listen on\n\n        server_name localhost; # give the server a name\n\n        location / {\n    \n            root /usr/share/nginx/html; # tell the server where its root folder is for all requests\n            index index.html; # tell the server which file extension or file type to use when not file have been provided in a folder\n        }\n\n    }\n}\n```\n\n## Update Dockerfile\n\nNow that we have learnt more about the nginx configuration we can update our dockerfile to have a custom nginx container\n\nFirst create a `nginx.conf` in the service folder and add the following code to it\n\n```config\nevents {\n    # and event contexts go here\n}\n\nhttp {\n\n    server {\n    \n        listen 80; # port which we want to listen on\n\n        server_name localhost; # give the server a name\n\n        location / {\n    \n            root /usr/share/nginx/html; # tell the server where \n                                        # its root folder is for all requests\n            \n            index index.html; # tell the server which file extension or \n                              # file type to use when not file have been provided in a folder\n        }\n\n    }\n}\n```\n\nSo as to make our own website we need to make a root folder to copy over to the nginx container.\n\nMake a directory called web in the service folder and create `index.html` and save it in the web folder with the following content:\n\n```html\n\u003ch1\u003eHello, world\u003c/h1\u003e\n```\n\nUpdate your dockerfile to look like this\n\n```Dockerfile\nFROM nginx\n\nCOPY nginx.conf /etc/nginx/nginx.conf\n\nCOPY web /usr/share/nginx/html\n\n```\n\n## House keeping\n\nNow that we have researched everything which we need to know a little house keeping is required.\n\nrun: \n\n```bash\n$ sudo docker ps\n```\n\nYou should see that your webserver container is running\n\n```bash\nCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES\nddd6bd45afc9        webserver           \"nginx -g 'daemon ...\"   49 seconds ago      Up 48 seconds       0.0.0.0:8080-\u003e80/tcp   webserver\n```\n\nWe need to stop it so as it can take on our new changes to our Dockerfile image.\n\n\u003e this will stop the webserver from running\n\n```bash\n$ sudo docker stop webserver\nwebserver\n```\n\u003e you should now see that it exited\n\n```bash\n$ sudo docker ps -a \nCONTAINER ID    ...        STATUS                      PORTS               NAMES\nddd6bd45afc9    ...        Exited (0) 20 seconds ago                       webserve\n```\n\n### Build our image again to update it\n\n```bash\n$ sudo docker build -t webserver .\nSending build context to Docker daemon  6.144kB\nStep 1/3 : FROM nginx\n ---\u003e 3f8a4339aadd\nStep 2/3 : COPY nginx.conf /etc/nginx/nginx.conf\n ---\u003e Using cache\n ---\u003e 476feb349398\nStep 3/3 : COPY web /usr/share/nginx/html\n ---\u003e Using cache\n ---\u003e 6121bb2d10ff\nSuccessfully built 6121bb2d10ff\nSuccessfully tagged webserver:latest\n```\n\n### Running our new container\n\nPrior to running our new container we need to delete the old one so that we can update the containers image id. \n\n```bash\n$ sudo docker rm webserver\n```\n\nNow we can run our new image with its updates.\n\n```bash\n$ sudo docker run -d -p 8080:80 --name webserver webserver\nd436f6e133817876568f26592bfa242728360d732ed12d1e61e538403e838404\n```\n\nIf you open [http://localhost:8080](http://localhost:8080) in your browser and your should see a 'Welcome to nginx' webpage.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidthorn%2Fnginx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidthorn%2Fnginx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidthorn%2Fnginx/lists"}