name: Deployment with nginx

sort: 3

nginx ile deployment

Go halihazırda kendi kendine çalışabilen bir http sunucuya sahiptir. Fakat sunucumuzda loglama, CC ataklarına karşı savunma ve statik dosya sunucusu gibi bazı ekstra özellikler isteyebiliriz. nginx bir web server olarak bunları iyi yapmaktadır.

Bu yaklaşımda Go sadece fonksiyonelliğe ve uygulamanın mantığına odaklanabilir. Biz de nginx proxy ile birden fazla uygulamayı aynı anda deploy edebiliriz. Aşağıda iki uygulamanın 80 portunu paylaştığı ama değişik domainlere sahip olduğu bir senaryo göreceksiniz. Bu konfigürasyonda nginx kendisine gelen istekleri başka uygulamalara yönlendiriyor :

  1. server {
  2. listen 80;
  3. server_name .a.com;
  4. charset utf-8;
  5. access_log /home/a.com.access.log main;
  6. location /(css|js|fonts|img)/ {
  7. access_log off;
  8. expires 1d;
  9. root "/path/to/app_a/static"
  10. try_files $uri @backend
  11. }
  12. location / {
  13. try_files /_not_exists_ @backend;
  14. }
  15. location @backend {
  16. proxy_set_header X-Forwarded-For $remote_addr;
  17. proxy_set_header Host $http_host;
  18. proxy_pass http://127.0.0.1:8080;
  19. }
  20. }
  21. server {
  22. listen 80;
  23. server_name .b.com;
  24. charset utf-8;
  25. access_log /home/b.com.access.log main;
  26. location /(css|js|fonts|img)/ {
  27. access_log off;
  28. expires 1d;
  29. root "/path/to/app_b/static"
  30. try_files $uri @backend
  31. }
  32. location / {
  33. try_files /_not_exists_ @backend;
  34. }
  35. location @backend {
  36. proxy_set_header X-Forwarded-For $remote_addr;
  37. proxy_set_header Host $http_host;
  38. proxy_pass http://127.0.0.1:8081;
  39. }
  40. }