I'm working locally for the moment.
I have an NGINX configuration for nuxtwoo.example.com.
Whenver I visit nuxtwoo.example.com, I need it to proxy localhost:3000, which is working fine, however I also need it to pass the port :300.
location / {
proxy_pass http://localhost:3000;
}
What I need,
http://nuxtwoo.example.com -> proxy_pass : localhost: 3000 -> URL in browser, nuxtwoo.example.com:3000.
This will also need to for other params, such as nuxtwoo.example.com/blog, should go proxy_pass localhost:3000/blog, and the browser url should be nuxtwoo.example.com:3000/blog.
Can't seem to figure this one out.
You need to use an upstream
upstream http_backend {
server 127.0.0.1:8080;
keepalive 16;
}
server {
...
location /http/ {
proxy_pass http://http_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
...
}
}
Related
The below nginx config is working fine if I hardcode my herokuapp(backend API) in proxy_pass section:
http {
server {
listen 8080;
location / {
proxy_pass http://my-app.herokuapp.com;
}
}
}
events { }
However if I try to add this in the upstream directive, its going to 404 page. I want to add this in upstream directive because I have other herokuapps as well where I want to load balance my requests.
This is the config which is not working:
http {
upstream backend {
server my-app.herokuapp.com;
}
server {
listen 8080;
location / {
proxy_pass http://backend;
}
}
}
events { }
These are all the things I tried after checking other SO answers:
add Host header while proxy passing. proxy_set_header Host $host;
add an extra slash at the end of backend.
In upstream directive, add server my-app.herokuapp.com:80 instead of just server my-app.herokuapp.com
In upstream directive, add server my-app.herokuapp.com:443 instead of just server my-app.herokuapp.com. This gives timeout probably because heroku doesn't allow 443(or maybe I didn't configure it).
Found the Issue: I was adding the wrong host. For heroku, for some reason you need to add host header with value as exactly what your app name is.
If your herokuapp name is my-app.herokuapp.com, then you need to add this line for sure:
proxy_set_header Host my-app.herokuapp.com;
Full working config below:
http {
upstream backend {
server my-app.herokuapp.com;
}
server {
listen 8080;
location / {
proxy_pass http://backend;
proxy_set_header Host my-app.herokuapp.com;
}
}
}
events { }
I have multiple backend servers and I want to proxy all servers using a single nginx server instance. I don't want to change nginx.conf whenever I add a new backend server.
For example: Server 1 : 192.168.10.1:8080, Server2: 192.168.10.2:8080, etc
Nginx is running on example.com. I want to access Server1 by using example.com?ip=192.168.10.1, example.com?ip=192.168.10.2 etc
I tried this configuration, but it is giving 500 error page.
location / {
proxy_pass http://$arg_ip:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
Is there something I am missing? Is there any other way of achieving this?
server {
server_name dynamic_host;
listern 8080;
#resolver 8.8.8.8;
#seems you don't need resolver because you use ip address
location / {
if ( $arg_address != "" ) {
proxy_pass $arg_address;
#proxy_pass $arg_address$uri
#proxy_pass $arg_address$request_uri
}
}
}
the difference between the three proxy_pass
$proxy_address
example.com?address=http://192.168.10.2:8080/ goes to
http://192.168.10.2:8080/
$proxy_address$uri
example.com/test/path?address=http://192.168.10.2:8080/ goes to
http://192.168.10.2:8080/test/path
$proxy_address$request_uri
example.com/test/path?address=http://192.168.10.2:8080/¶m=value goes to
http://192.168.10.2:8080/test/path?address=http://192.168.10.2:8080/¶m=value
you can change the param address to ip, in this case, don't forget to change $arg_address to $arg_ip.
reference:
http://nginx.org/en/docs/http/ngx_http_core_module.html#variables
We have a handful of homogenous application loosely under SOA pattern. Because of homogeneity, we have been able to define some neat pattern in Nginx to proxy all of our SOA apps through one configuration. Following Nginx configuration is absolutely working absolute wonders in conjunction with DNSmasq to resolve anything.yourdomain.devel eg. a.stackoverflow.devel, b.stackoverflow.devel domains and route that to appropriate app servers under your project folder via designated ports via maps.
worker_processes 2;
events {
worker_connections 1024;
}
http {
map $host $static_content_root {
hostnames;
default /path/to/project/folder;
# For typical standalone apps living in your project directory
# *.myapp.local.devel -> /path/to/project/myapp/public
~^([^\.]+\.)*(?<app>[^\.]+)\.devel$ /path/to/project/folder/$app/public; #rails pattern
}
map $app $devel_proxy_port1 {
default 3000;
domain1 3000;
domain2 4000;
}
map $app $devel_proxy_port2 {
default 3001;
domain1 3001;
domain2 4001;
}
server {
listen 127.0.0.1;
server_name ~^([^\.]+\.)*(?<app>[^\.]+)\.[^\.]+.devel$;
location / {
root $static_content_root; # Using the map we defined earlier
try_files $uri $uri/index.html #dynamic;
}
location #dynamic {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forward-Proto http;
proxy_set_header X-Nginx-Proxy true;
proxy_redirect off;
proxy_pass http://127.0.0.1:$devel_proxy_port1;
}
}
}
Now, in order to simulate multiple servers behind Nginx load balancer. I thought of doing following proxy configuration which points to upstream rather than directly pointing to one server:port pair.
proxy_pass http://backend;
upstream backend {
server http://127.0.0.1:$devel_proxy_port1;
server http://127.0.0.1:$devel_proxy_port2;
}
I thought above would work but it always emits following error hinting the variables of map blocks are not available inside upstream context.
[emerg] 69478#0: invalid host in upstream "http://127.0.0.1:$devel_proxy_port1" in /usr/local/etc/nginx/nginx.conf:57
Is this an expected behavior?
Yes, variable can not be used inside upstream. You can create few upstream blocks with different names (upstream backend, upstream backend_domain, etc), resolve upstream name through map and put this variable to proxy_pass:
upstream backend {
server http://127.0.0.1:3000;
server http://127.0.0.1:3001;
}
upstream backend_domain1 {
server http://127.0.0.1:3002;
server http://127.0.0.1:3003;
}
upstream backend_domain2 {
server http://127.0.0.1:3004;
server http://127.0.0.1:3005;
}
...
upstream backend_domain30 {
server http://127.0.0.1:3060;
server http://127.0.0.1:3061;
}
map $app $devel_proxy {
default backend;
domain1 backend_domain1;
domain2 backend_domain2;
...
domain30 backend_domain30;
}
...
proxy_pass $devel_proxy;
...
In some cases you can skip map block using $app inside proxy_pass: proxy_pass backend_$app;, but need additional checks for $app values. Also, map allow to to map different "domains" to same applications.
I am trying to setup an nginx proxy in front of any s3 website that I point at it. In the example below I have my DNS records customsite.com pointing to my proxy. When I don't use any variable for the destination of the proxy_pass there isn't an issue, but I want to be able to just dynamically pass the request using $host. How do I get the $host variable to behave with proxy_pass?
This Works
server {
listen 80;
location / {
add_header RequestedHost $host; # The host is returned as expected (customsite.com)
# go get it from s3
proxy_pass http://customsite.com.s3-website-us-east-1.amazonaws.com;
}
}
This Doesn't Work
server {
listen 80;
location / {
add_header RequestedHost $host;
# go get it from s3
proxy_pass http://$host.s3-website-us-east-1.amazonaws.com; # Doesn't resolve
}
}
I have a nginx.conf which basically looks like (unnecessary parts omitted):
upstream app {
server unix:/tmp/unicorn.myapp.sock fail_timeout=0;
}
server {
listen 80;
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
}
I want to configure nginx so that the value of a specific header is used to rewrite the url being passed to the upstream.
For example, let's assume that I have a request to /test with the header Accept: application/vnd.demo.v1+json. I'd like it to be redirected to the upstream URL /v1/test, i.e. basically the upstream app will receive the request /v1/test without the header.
Similarly, the request to /test and the header Accept: application/vnd.demo.v2+json should be redirected to the upstream URL /v2/test.
Is this feasible? I've looked into the IfIsEvil nginx module, but the many warnings in there made me hesitant to use it.
Thanks,
r.
edit
In case there's no match, I'd like to return a 412 Precondition Failed immediately from nginx.
If Accept header does not contain required header return error.
map $http_accept $version {
default "";
"~application/vnd\.demo\.v2\+json" "v2";
"~application/vnd\.demo\.v1\+json" "v1";
}
server {
location #app {
if ($version = "") {
return 412;
}
...;
proxy_pass http://app/$version$uri$is_args$args;
}
}