location "/app" cannot be inside the named location - nginx

I want to configure a nginx reverse-proxy server to redirect requests to different servers depending on :
the endpoint
whether it is a plain web request or a websocket upgrade request
I know I can use locations to manage the first point, and named locations to manage the second point, but how can I do both?
server {
listen 80 default_server;
listen [::]:80 default_server;
location /app {
location #web {
proxy_pass http://127.0.0.1:9080/app;
}
location #ws {
proxy_pass http://127.0.0.1:9081;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
}
I get the error message location "/app" cannot be inside the named location "#web"
What am I supposed to do to managed that kind of mixed trafic

Related

Nginx wrongly routes to subdomain instead of "main" domain

I have a setup where Nginx acts as an entrance to some other services down the line. Specifically, there are multiple apps and a WordPress instance running in docker containers. My configuration looks like this:
server {
listen 80 default_server;
server_name domain.com;
location / {
resolver 127.0.0.11 valid=30s;
set $upstream_wp wordpress;
proxy_set_header Host $host;
proxy_pass http://$upstream_wp;
proxy_redirect off;
}
}
server {
listen 80;
server_name app1.domain.com;
location / {
resolver 127.0.0.11 valid=30s;
set $upstream_app1 app1;
proxy_set_header Host $host;
proxy_pass http://$upstream_app1;
proxy_redirect off;
}
}
What happens now is the following:
domain.com: gets routed to app1 (should be wordpress)
domain.com/anything: gets routed to wp (correct)
app1.domain.com: gets routed to app1 (correct)
app1.domain.com/anything: gets routed to app1 (correct)
I don't understand why the first one does get routed to the app. Any suggestions?

Flask API & nginx alongside each other

I have a server that I'm trying to set up. I have a Flask server that needs to run on api.domain.com, while I have other subdomains pointing to the server. I have one problem. 2/3 subdomains have no problem using nginx. Meanwhile, my script tries to bind to port 80 on the same machine, therefore failing. Is there a way I can bind my Flask REST script to port 80 ONLY for the subdomain 'api'?
My current config is:
server {
server_name api.domain.me;
location / {
error_page 404 /404.html;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_max_temp_file_size 0;
proxy_pass http://127.0.0.1:5050/;
proxy_cache off;
proxy_read_timeout 240s;
}
}
There's a little problem though, nginx likes to turn all POST requests into GET requests, any ideas?
Thanks!
There is no way binding two different applications on port 80 at the same time.
I would set up your api like this:
Bind your Flask API to Port 8080.
On NGINX you can configure you subdomain pointing to your Flask Application
upstream flask_app {
server 127.0.0.1:8080;
}
sever {
listen 80;
server_name api.domain.com;
location / {
proxy_pass http://flask_app/;
proxy_set_header Host $host;
}
}
I actually found out after a bit of diagnosis.
server {
if ($host = api.domain.me) {
return 301 https://$host
}
# managed by Certbot
had to become:
server {
if ($host = api.domain.me) {
return 497 '{"code":"497", "text": "The client has made a HTTP request to a port listening for HTTPS requests"}';
}
Because Certbot tries to upgrade the request to https but the HTTP method gets changed to GET because of the 301 response code.

how to redirect my domain to localhost: 3000 using ngnix

I'm new to all of this.
I'm going to put you in context. I bought a domain miweb.pe and an instance in aws. Currently my domain redirects to my aws instance because I have registered the dns servers of my amazon instance in myweb.pe.
I bought an ssl certificate and am trying to install it on my amazon instance, where I also installed nginx. I am unable to make any request to myweb.pe redirect to the aws instance that currently has a nodejs service active under port 3000.
this is my current configuration. What am I doing wrong?
server {
listen 443;
server_name myweb.pe;
ssl on;
ssl_certificate /etc/ssl/ssl-bundle.crt;
ssl_certificate_key /etc/ssl/beekey.key;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 80;
return 301 https://$host$request_uri;
}
server {
listen 80;
server_name www.myweb.pe;
return 301 https://myweb.pe$request_uri;
}
# Redirige de https://www.tudominio.com a https://tudominio.com
server {
listen 443;
server_name www.miweb.pe;
return 301 $scheme://myweb.pe$request_uri;
}
in summary, I want that when accessing myweb.pe it actually accesses thelocalhost: 3000 which is running on my amazon instance.
So, what is the issue you are facing, I can see one issue in your nginx rule for servername you need to type domain name and not localhost. The other thing is I am assuming your service on port 3000 should already be running.

NGINX not routing calls from react app to backend app

I have an AWS Ubuntu server that hosts a react front end running at 127.0.0.1:4100 and makes api calls to a Go app using port 127.0.0.1:1323. I installed Nginx and setup proxy pass for these two ports in /etc/nginx/sites-available/default config file but I only get the front end getting called by Nginx. Using chrome inspect to check why the Go app is not serving some of the functionalities from the react app, I see this error
client.js:772 GET http://127.0.0.1:1323/api/ net::ERR_CONNECTION_REFUSED
ERROR Error: Request has been terminated Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.
What am I doing wrong? Below is my default config file
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location / {
proxy_pass http://127.0.0.1:4100;
}
location /api {
proxy_pass http://127.0.0.1:1323/;
}
}
Your server is listening to port 80:
listen 80 default_server;
listen [::]:80 default_server;
So, you should make your request to that port:
GET http://127.0.0.1/api/ => http://127.0.0.1:1323/
GET http://127.0.0.1:80/api/ => http://127.0.0.1:1323/
GET http://127.0.0.1/ => http://127.0.0.1:4100/
GET http://127.0.0.1:80/ => http://127.0.0.1:4100/
Then nginx should correctly proxy your requests.
Update
To be more clear about nginx config.
server {
listen 80 default_server; // The port nginx is listening to ipv4
listen [::]:80 default_server; // The port nginx is listening to ipv6
server_name _;
location / { // When you call this location...
proxy_pass http://127.0.0.1:4100; // You'll be redirected to this location
}
location /api { // When you call this location...
proxy_pass http://127.0.0.1:1323/; // You'll be redirected to this location
}
}
Your configuration is okay according to nginx docs.
You said your client is trying to reach http://127.0.0.1:1323/api/ but It should be requesting http://127.0.0.1/api/ (whitout the port) to be redirected to http://127.0.0.1:1323/.
Here's another example:
server {
listen 80;
server_name localhost anywebsite.com;
location ~* ^/MyApp {
proxy_pass http://localhost:5130;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_send_timeout 2m;
proxy_read_timeout 2m;
}
}
In this case, everytime my url ends with /MyApp ex.: http://anywebsite.com/api/MyApp I'm being proxyed to http://localhost:5130. But if I try to access http://localhost:5130 or http://anywebsite.com:5130/api/MyApp I won't be able because nginx is listening to port 80 only. If you want to access another port you need to specify like this:
server {
listen 80;
listen 5130;
[...]

proxy pass configuration in nginx

I am a front end developer and tried my hands in nginx configuration last time which is working fine. The below is the configuration:
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
location / {
#By default route to node.js running on localhost:9000 port
proxy_pass http://localhost:9000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
#currently only one server but will have to redirect to n hosts based on a parameter
location /hosts.json {
proxy_pass http://app-host.net:3000;
}
#currently only one server but will have to redirect to n hosts based on a parameter
location /hosts/ {
proxy_pass http://app-host.net:3000;
}
}
Now, I need to redirect to 4 different servers based on a parameter. ie if the city is Bangalore, I need to redirect to bangalore.corp.net:3000 and if the city is NewYork, then I need to redirect to newyork.corp.net:3000 and so on.
Here is somewhat I am expecting:
location /app1/hosts/ {
proxy_pass http://app1-host.net:3000;
}
#But the proxy pass should point to http://app1-host.net:3000/hosts and not http://app1-host.net:3000/app1/hosts
How can we handle such proxy pass in the nginx configuration file. Please let me know.
You have a URL of the form /app1/hosts/foo which should map to http://app1-host.net:3000/hosts/foo. The can be achieved by appending a URI in the proxy_pass directive, which will act like an alias.
location /app1/hosts/ {
proxy_pass http://app1-host.net:3000/hosts/;
}
See this document for details.

Resources