Nginx reverse proxy + Docker mount volume

Today I did two things for my blog project: added a proxy on my Nginx server for the api connection and mounted /data/db directory from the host to the docker container to achieve data persistency.

First, Nginx proxy.

The idea/goal isn’t that complicated. There are three docker containers running on my production machine for the blog, by service names:

  1. blog-api, the api server that listens to :1717
  2. web, the frontend nginx server that listens to :80
  3. mongodb, the mongodb database that listens to :27017

Before today, I had to configure the port in the frontend code, so that the frontend calls the api endpoints with the base url and the port number. If this didn’t bother me enough, for the image uploading, all of the urls for embedding the images in the posts have the port numbers in them, so they look like this:

vcm-3422.vm.duke.edu:1717/uploads/image.png

It is against intuition for the port number to be shown to users, so I began looking for a solution. Nginx turns out to have this reverse proxy configuration that allows you to proxy requests to some location to some other port number, or even a remote server. It’s called “reverse” proxy because unlike “normal” proxies, the nginx server is the “first server” that the client connects to, whereas in other proxies the “proxy server” is the first and the nginx server would be behind the proxy.

With some trial and error, I came to this:

http {
	upstream docker-api {
		server blog-api:1717;
	}

	server {
		listen 80;
		root /app/dist;
		location / {
			try_files $uri /index.html;
		}

		location /api {
  			rewrite /api/(.*) /$1  break;
			proxy_pass http://docker-api;
			proxy_set_header X-Forwarded-Host $server_name;
			proxy_set_header X-Real-IP $remote_addr;
		}
	}
	
	# other configs
}

The upstream server name following the upstream keyword can be arbitrary, but the server name blog-api matches with my docker service name and will serve as the host name for the second hop as shown in the proxy_pass field below.

The location /api block does the proxying. The first line rewrites the request so that the /api part of the url is stripped since “api” is only used for triaging. The next few lines are pretty standard. They basically send along the original headers.

Voila! When I built and up’d my docker-compose services, I can see the blog posts showing up just like before. However, when I randomly tested image upload, I got a Request Entity too Large 413 error in the browser console. Apparently this is caused by the new nginx config, but how?

After some Googling, it turns out that nginx has a setting in HTTP server called client_max_body_size, which defaults to 1M. What’s more, from the documentation it says any request with body larger than this limit will get a 413 error, and this error cannot be properly displayed by browsers! Okay… so in the server block I added a

client_max_body_size 8m;

and everything works just fine 👌.

 

For the docker volume configuration, I came up with this requirement for myself because my mongodb data had been stored in the docker container – it gets lost when the container is removed. To back up the data easier and to have more confidence in the data persistency, I wanted to mount the database content from the host machine instead.

So in my docker-compose.yml,I added this simple thing to the mongodb service:

volumes:
      - /data/db:/data/db

Yes two lines and I included in my post. What can you do about it 🤨

Now I can just log in my production machine and copy away the /data/db directory to back up my precious blog posts data 🙂

 

Reference:

Use NGINX As A Reverse Proxy To Your Containerized Docker Applications

187 thoughts on “Nginx reverse proxy + Docker mount volume”

  1. “Nice read, I just passed this onto a friend who was doing some research on that. And he actually bought me lunch because I found it for him smile Therefore let me rephrase that Thanks for lunch!”

  2. I enjoy the valuable data a person source for your content articles. Let me take note of your current blog page and check out all over again in this article usually. I will be quite specific I’m going to be advised many brand new information appropriate listed here! All the best kumpulan youtube terbaik. for the following!

  3. “I’m not that much of a online reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your site to come back in the future. Cheers”

  4. Hi there. I found your own weblog using bing. It is a actually logically published write-up.. rekomendasi saham terbaru I am going to ensure that you search for this as well as go back to find out more of one’s helpful tips. Many thanks for a submit. Let me certainly return.

  5. I must show my thanks to the writer just for rescuing me from this particular setting. As a result of surfing throughout the online world and getting techniques that were not powerful, I assumed my life was gone. Existing without the presence of answers to the difficulties you’ve sorted out by means of your entire website is a serious case, and the ones which could have adversely affected my entire career if I hadn’t encountered your site. The natural talent and kindness in maneuvering a lot of stuff was precious. I don’t know what I would have done if I had not encountered such a thing like this. I’m able to at this moment look forward to my future. Thank you very much for your professional and effective help. I will not be reluctant to propose your site to any person who requires assistance on this area.

  6. I am glad for writing to make you understand what a excellent discovery my girl went through going through your web site. She discovered such a lot of things, not to mention how it is like to have an excellent helping heart to let the rest very easily have an understanding of a variety of hard to do topics. You undoubtedly exceeded readers’ desires. Thanks for churning out these necessary, healthy, edifying and also fun guidance on the topic to Sandra.

  7. I in addition to my friends have already been following the great helpful tips located on your web site and then the sudden I had a terrible suspicion I never thanked the blog owner for those tips. The young boys became totally stimulated to learn all of them and have simply been having fun with them. We appreciate you indeed being quite considerate and also for finding this kind of important ideas most people are really eager to learn about. Our sincere apologies for not expressing gratitude to you earlier.

  8. “Thank you a bunch for sharing this with all folks you really know what you are speaking approximately! Bookmarked. Kindly also visit my website =). We could have a link trade agreement among us”

  9. Wonderful website. Many beneficial information and facts in this article. I’m mailing that to a couple good friends ans additionally giving within scrumptious. And obviously, thanks a lot on your energy! Wonderful website. Many beneficial information and facts in this article. I’m mailing that to a couple good friends ans additionally giving within scrumptious. And obviously, thanks a lot on your energy!

  10. Are you able to add a Blackberry format? This particular web page is actually difficult to read Nginx reverse proxy Docker mount volume – Bill Yu or else for those people surfing around along with mobile devices. Normally, in case you can put a new RSS sign up, that could be great also. Web Address http://feraripk.net/

  11. Stick to call at your interest in the do the job you’re posting. The world desires of extra zealous authors like you who seem to will not be scared to note that they believe that. Continuously comply with the cardiovascular aplikasi berita saham terbaru.

  12. Dear you are comprehensive in your thoughts. I have made something for guys to find their life partner. Please check my blog and share with people who need this guide.

  13. 由全球生長因子與胜肽韓國廠商CAREGEN研發,利用美國專利「KP7-4胜肽技術」,獨特滲透配方提供高穩定、高滲透、高效能的胜髮環境,模擬人體生髮機制,目的使頭皮、毛囊恢復健康及作用活性,重啟生髮系統。DR CYJ髮胜肽療程的治療原理為 利用複合活性成分配方恢復毛髮平衡, 根據生態生物工程原理研發, 關鍵胜肽名為KP7-4,結合了七種專利胜肽, 實驗證明能有效模擬「人體胜髮因子」,掌握育髮四關鍵:毛囊強健、減緩異常落髮、強化髮根、供給毛囊血管營養,模擬毛髮循環機制,同時減緩異常落髮及強化髮根。DR.CYJ髮胜肽不含藥性且無副作用,能有效促進增髮

  14. I spent four years trying EVERYTHING in Online Dating, and through a huge amount of trial and error, I produced a system that I will share for you. This book will take you, step by step, through everything you need to know to double, triple or even quadruple the number of women you meet online.

  15. We appreciate you various other useful web page.. cara menjadi investor The site in addition may I buy that kind of details printed in this type of perfect strategy? I’ve a enterprise that we are basically now taking care of, and I’ve also been within the glance outside intended for this kind of info.

  16. 前額拉皮、提眉手術、抬頭紋、三角眼、眉眼下垂、眉尾下垂、眼尾下垂、眉尾提拉,雙眼皮手術等一些相關議題、似是而非或有爭議的事項的披露與討論。

  17. Sculptra 含有由含有源於植物果酸的 PLLA 聚左乳酸(poly-L-lactic acid),它能促進天然膠原蛋白大量增生,令容顏重現年輕,一個療程的效果能持續逾2年。Sculptra 乃全球唯一獲美國食物及藥物管理局(美國FDA)認可的PLLA聚左乳酸美容品牌。Sculptra 塑然雅會被人體自然分解吸收,同時促進天然膠原蛋白再生1。療程效果可以漸進地給你更富清新朝氣的容頻,效果自然,更不會影響面部表情。SCULPTRA 塑然雅 Sculptra 由含有源於植物果酸的PLLA聚左乳酸(poly-L-lactic acid),它可以被新陳代謝成二氧化碳及水,最終自體內排出。PLLA聚左乳酸早已被廣泛應用於醫療用品超過30年,自2009年起 Sculptra 及全球唯一獲美國FDA認可的PLLA聚左乳酸美容醫學品牌。Sculptra ® 塑然雅能促進天然膠原蛋白大量增生1,改善皺紋,令容顏重現年輕。全球唯一獲美國FDA認可PLLA聚左乳酸。臨床研究證實於3個月間,肌膚的膠原蛋白密度 顯著增加67%2。療程效果能持續愈2年3。主要成份PLLA聚左乳酸源於植物果酸,醫學界廣泛使用超過30年。

Leave a Reply

Your email address will not be published. Required fields are marked *