Nginx proxy_pass to dynamic url captured with regex - nginx

I'm trying to set a CORS proxy using Nginx.
My application runs on localhost:8080 (as a docker container) with Nginx. It calls some external resources thus facing CORS issues (remote servers do not have CORS enabled).
Here is the docker-compose file :
version: '3'
services:
myapp:
image: myimage
ports:
- 8080:80
volumes:
- ./apps:/usr/share/nginx/html/apps
- ./default.conf:/etc/nginx/conf.d/default.conf
And here is the default.conf file :
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location ~ /proxy/(.*) {
proxy_pass https://$1$is_args$args;
access_log /var/log/proxy_access.log;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
So my application calls http://localhost:8080/proxy/https://external_domain.com?key=value to bypass CORS restriction.
Problem is there seems to be an issue somewhere in my configuration because the previous request returns 500 error with the following message in logs :
invalid port in upstream "https://external_domain.com?key=value", client: <container_ip>, server: localhost, request: "GET /proxy/external_domain.com?key=value HTTP/1.1", host: "localhost:8080"
I tried alternative solutions using request url params instead of regex, but with no more success.

Related

nginx proxy_pass working but static content doesn't

I can set up nginx as a reverse proxy with no major issues, but if I do a simple static page test like this, the server doesn't serve pages:
server {
server_name localhost;
listen 12345;
location / {
root /Volumes/E/static/;
index index.html index.htm;
}
}
error.log says:
2023/02/09 22:39:10 [crit] 53512#0: *18710 open() "/Volumes/E/static/index.html" failed (1: Operation not permitted), client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:12345"
...and I get a "500 Internal Server Error" in the browser when entering http://localhost:12345/.
I've tried chmod -R 755 /Volumes/E/static, no effect.
Why is that?

NGINX - Short domain proxy to full path on another domain

I have two domains, let's say ex.io and example.com. I own both and have access to both servers. Both run NGINX.
Goal: I'd like to have any request from ex.io forward to a specific path under example.com, e.g. http://ex.io passes to https://example.com/foo/bar. This has to be done without a redirect (more in Context)
Context: The goal is to host a shell script at https://example.com/foo/bar so that curl ex.io | sh will run the shell script. I'd like no redirects to happen so no additional flags are needed for curl.
My current .conf setup for both servers follows:
server {
listen 80;
listen [::]:80;
server_name example.com
return 308 https://$host$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name ex.io;
return 308 https://example.com/foo/bar;
}
# ...
This works, but requires the extra flag -L to run properly under curl. How can I proxy ex.io to go directly to https://example.io/foo/bar?
EDIT: I'm forwarding http://ex.io/ to https://example.com/foo/bar which may be tricky going from http to https. Bananenkönig's response fails with a 502 Bad Gateway error and the following logs:
2020/10/26 23:28:45 [error] 223#223: *281 SSL_do_handshake() failed (SSL: error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure:SSL alert number 40) while SSL handshaking to upstream, client: XXX.XXX.XXX.XXX, server: ex.io, request: "GET / HTTP/1.1", upstream: "https://XXX.XXX.XXX.XXX:443/foo/bar/", host: "ex.io"
2020/10/26 23:28:45 [warn] 223#223: *281 upstream server temporarily disabled while SSL handshaking to upstream, client: XXX.XXX.XXX.XXX, server: ex.io, request: "GET / HTTP/1.1", upstream: "https://XXX.XXX.XXX.XXX:443/foo/bar/", host: "ex.io"
I would try it like this:
server {
listen 80;
listen [::]:80;
server_name ex.io;
location /some/location/on/ex.io/ {
proxy_pass https://example.com/some/location/on/example.com/;
}
}
when you want ex.io/ (on location /) to be forwarded to example.com/... write "location / { "

Redirect /api requests to port 3000 using NGINX (Docker)

I want to redirect the requests to the /api to the node app running on localhost:3000 using NGINX
I'm trying to redirect the requests using proxy_pass
server {
listen 80;
server_name localhost;
root /var/www/mydomain/html;
index index.html index.htm;
location /api {
proxy_pass http://127.0.0.1:3000/;
}
}
If I access localhost in my browser I see the frontend application, so that's correct.
If I hit http://127.0.0.1:3000 (or localhost:3000) I get a response from the node API, so that's correct.
But when I try to access localhost/api I get the following error:
[error] 6#6: *3 connect() failed (111: Connection refused) while connecting >to upstream, client: 172.18.0.1, server: localhost, request: "GET /api >HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "localhost"
EDIT:
I'm using a docker-compose to manage the services:
services:
nginx:
container_name: nginx-local
build:
context: .
dockerfile: ./nginx.dev.dockerfile
volumes:
...
ports:
- '80:80'
networks:
- local-network
node:
container_name: node-api
build:
context: ./api
dockerfile: .docker/node.dockerfile
ports:
- '3000:3000'
networks:
- local-network
Based on the docker-compose file the nginx config for needs to be changed to
server {
listen 80;
server_name localhost;
root /var/www/mydomain/html;
index index.html index.htm;
location /api {
proxy_pass http://node-api:3000/;
}
}
This is due to the separate docker image for the node application. So you need to use the proxy pass to point to the correct location.

nginx proxy_pass to flask works while /static fail to find jquery

I'm a noob an nginx (and apache and php ...)
I've this flask app that works fine from:
http://127.0.0.1:5000
and also externally from
http://myhost.com:5000
I would like to use that from
myhost.com/rest_1
to make room to others rest, like myhost.com/rest_2 .. 3 and so on.
The app resides in
/opt/rest_1
and uses some resources from it's /static folder like css and jquery.
I've this
/etc/nginx/sites-available/rest_1.conf
server {
listen 80;
listen [::]:80;
server_name myhost.com;
server_name_in_redirect off;
location /rest_1 {
rewrite ^/rest_1(.*) /$1 break;
proxy_pass http://127.0.0.1:5000/;
}}
and it's link to sites-enabled.
nginx restart and reload with no errors.
Other configurations are default from installation.
When i try to connect to
myhost.com/rest_1
I can see an incomplete page partially working, looking at nginx log:
/var/log/nginx/error.log:
*8 open() "/usr/share/nginx/html/static/w3.css" failed (2: No such file or directory), client: xx.xx.xx.xx, server: myhost.com, request: "GET /static/w3.css HTTP/1.1", host: "myhost.com", referrer: "http://myhost.com/rest_1"
So, it is clear to me that '/usr/share/nginx/html/' is got from elsewhere... and should also have a proper name that piece of folder (I'm a noob!!)
How to tell to the engine to redirect to the correct path on
/opt/rest_1
to get back all /static functionalities ?
OS: ubuntu server 16.04
nginx: 1.10.3
Thanks.
Cause you don´t set one location resource for your static content. Your app is referencing an CSS from de root folder and not from rest_1.
Your CSS Call was http://myhost.com/STATIC, so it not match in the location that you set and try inside the default.
So you can solve it in your app or you can set it in your nginx as below:
location /static {
root /opt/rest_1;
}

uWSGI nginx error : connect() failed (111: Connection refused) while connecting to upstream

I'm experiencing 502 gateway errors when accessing my IP on nginx(http://52.xx.xx.xx/), the logs simply says this:
2015/09/18 13:03:37 [error] 32636#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: xx.xx.xx.xx, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8000", host: "xx.xx.xx.xx"
my nginx.conf file
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name xx.xx.xx.xx; # substitute your machine's IP address or FQDN
charset utf-8;
access_log /home/ubuntu/test_django/nginx_access.log;
error_log /home/ubuntu/test_django/nginx_error.log;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/ubuntu/test_django/static/media/; # your Django project's media files - amend as required
}
location /static {
alias /home/ubuntu/test_django/static/; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/ubuntu/test_django/uwsgi_params; # the uwsgi_params file you installed
}
}
Is there anything wrong with nginx.conf file.....if i use default conf then it is working.
I resolved it by changing the socket configuration in uwsgi.ini
from socket = 127.0.0.1:3031, to socket = :3031. I was facing this issue when I ran nginx in one Docker container and uWSGI in another. If you are using command line to start uWSGI then do uwsgi --socket :3031.
Hope this helps someone stuck with the same issue, during deployment of a Django application using Docker.
change this address:
include /home/ubuntu/test_django/uwsgi_params;
to
include /etc/nginx/uwsgi_params;
I ran into this issue when setting up the env by nginx + gunicorn and solve it by
adding '*' to ALLOWED_HOSTS or your specific domain.
In my case with a debian server it worked moving:
include /etc/nginx/uwsgi_params;
In the location tag in my nginx server config file, like this:
location /sistema {
include /etc/nginx/uwsgi_params;
uwsgi_pass unix://path/sistema.sock;
}
Also, check you have the following packages installed:
uwsgi-plugin-python
pip3 install uWSGI did the trick for me :D

Resources