Skip to content

Instantly share code, notes, and snippets.

@alibo
Last active November 5, 2018 08:07
Show Gist options
  • Save alibo/339fab2eb311d4f53a8b822297285aaa to your computer and use it in GitHub Desktop.
Save alibo/339fab2eb311d4f53a8b822297285aaa to your computer and use it in GitHub Desktop.
How to send traffic of some endpoints to a different app in Nginx (php + golang)
upstream @api_golang {
server 127.0.0.1:8080; # same node
server 111.222.111.222:8080;
}
upstream @api_php {
server 127.0.0.1:80; # nginx/apache/... - another node
server 111.222.111.222:80; # nginx/apache/... - another node
}
server {
# ...
location = /static/route/rewritten-with-golang/ {
proxy_pass http://@api_golang/static/route/rewritten-with-golang/;
proxy_set_header Host "domain.com"; # if you use host!
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~* \/dynamic-golang/.*/with-regex$ {
rewrite /(.*) /$1 break;
proxy_pass http://@api_golang/;
proxy_set_header Host "domain.com"; # if you use host!
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# if php-fpm and nginx are in the same node
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# if you have a main load balancer in a dedicated node
location / {
proxy_pass http://@api_php;
proxy_set_header Host "domain.com";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}