NGINX reverse proxy not forwarding the request hostname to docker container - docker

Problem:
We've setup a docker container running on port 3002 and then configured port 3002 to /path/ on my domain www.example.com. There's an express rest api is running on 3002 port container which outputs the req.hostname and when I make a request from let's say www.abc.com, the consoled value of req.hostname is shown to be www.example.com instead of www.abc.com.
Nginx Conf
server {
listen 443 ssl;
ssl_certificate /etc/ssl/__abc.crt;
ssl_certificate_key /etc/ssl/abc.key;
listen 80 default_server;
listen [::]:80 default_server;
location / {
proxy_pass http://localhost:3001/;
proxy_set_header Host $host;
}
location /path/ {
proxy_pass http://localhost:3002/;
proxy_set_header Host $http_host;
}
}
What changes do I have to make so I can get the www.abc.com in consoled value?

Nginx's location blocks should be ordered such that more specific expressions come first.
In your example, you should have:
location /path/ {
proxy_pass http://localhost:3002/;
proxy_set_header Host $http_host;
}
location / {
proxy_pass http://localhost:3001/;
proxy_set_header Host $host;
}
Make sure your changes take effect by either running nginx -s reload or restarting the container

Related

How can I access Flask app through NGINX via Docker

I have a very simple Docker container, running NGINX and Flask...
My NGINX instance is the front door, which then reverse proxies traffic through to the Flask app.
My issue currently is that I can access the flask app locally, through the Docker internal ip: 172.18.0.2:5000 BUT not as my NGINX instance is configured, I want to access it through localhost:5000.
My code listed is my current nginx.conf file. I have tried different variations of this...However with no luck. Any assistance will be appreciated, How I could access it through localhost, or with my host PC ip.....As mentioned, I cannot figure out why its only accessible through the Docker internal container IP
server {
listen 443 ssl;
listen [::]:443 ssl;
listen 5000;
server_name localhost;
ssl_certificate /root/ssl/cert.pem;
ssl_certificate_key /root/ssl/key.pem;
location / {
proxy_pass "http://localhost:5000/";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
error_page 500 502 503 504 /50x.html;
}

How to access local backend services to a deployed Web app using nginx?

I'm deploying some services using Docker, with docker-compose and a nginx container to proxy to my domain.
I have already deployed the frontend app, and it is accessible from the web. Supposedly, I only need to expose the frontend/nginx port to the web, without me needing to expose the rest of the services. But I'm not able to do that for now.
For example, Client -> Login Request -> frontend <-> (local) Backend get request.
Right now, I'm getting connection refused, and the get is pointing to http://localhost, and not the name of the service defined in docker-compose. (all containers are deployed on the same network, one that I have created)
What do I need to do to configure this?
Here is my nginx config so far:
server {
listen 80 default_server;
listen 443 ssl;
server_name mydomain;
ssl_certificate /etc/letsencrypt/live/mydomain/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomain/privkey.pem;
location / {
proxy_pass http://frontend:3000/;
}
location /auth {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://auth:3003;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
# Same for the other services
(...)
EDIT:
Should I create a location for every get and post that I have for my services?
As:
location getUser/ {
proxy_pass http://auth:3003;
}

Is it possible to send webhook through NGINX proxy

I have a setup where i can access to my docker containers through nginx proxy. All containers are in internal network and does not have access to the internet.
Now I need to send webhook to slack from my docker container. In container i edited file "/etc/hosts", just added a record to my proxy, which is similiar to hooks.slack.com. I did this, because container sending request to proxy, after that proxy get it and redirects to the internet original hooks.slack.com.
server {
listen 80;
server_name hooks.slack.com;
return 301 https://hooks.slack.com$reques_uri;
}
When i am testing this by CURL from my docker container i get answer 301 and nothing happened, no message delivered to slack.
SOLVED:
Needed to add this intead of my actual setup.
server {
listen 80;
server_name hooks.slack.com
location / {
proxy_pass https://hooks.slack.com;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
}
}
Use proxy_pass instead of 301 redirect
server {
listen 80;
server_name hooks.slack.com;
location / {
proxy_pass https://hooks.slack.com;
}
}

NGINX reverse proxy to Apache docker container 404

I'm trying to host several websites on my droplet. I'm to do that, I'm using NGINX (not container) as reverse proxy to Dockerized apps. One such app I'm using is the dockerized Mediawiki set to run on 0.0.0.0:8081.
Mediawiki container is based on php7.2-apache.
Nginx configuration :
server {
listen 443 ssl;
index index.php;
server_name my.website.com;
ssl_stapling on;
ssl_stapling_verify on;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://0.0.0.0:8081;
}
ssl_certificate /etc/letsencrypt/live/my.website.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/my.website.com/privkey.pem; # managed by Certbot
}
I run the application on port 8081, as can be seen by through docker ps -a
CONTAINER IMAGE PORTS
e40c9815d6cc mediawiki 0.0.0.0:8081 -> 80/tcp
I can access my.website.com, but it shows the default Apache Ubuntu default page. Accessing other pages and resources (index.php, /folder/index.php, images/pic.jpg) returns 404.
Testing the container with similar setup on my machine local works. I think there maybe something up I didn't get with the NGINX config.
Help?

How to set my IP address to default url for my page in Nginx

I want to set my AWS instance's ip address(e.g. 52.172.33.23) to my default page, which means when I put 52.172.33.23 on web browser, my application works without server_name. So, I set /opt/nginx/conf/nginx.conf like,
server {
listen 80 default_server;
passenger_enabled on;
root /home/ec2-user/my_app/public;
}
But server running works with sudo /opt/nginx/sbin/nginx, but nothing shows on my ip address.
Additionally, I opened port 3000, and changed listen 80 default_server; to listen 3000 default_server; it worked on 52.172.33.23:3000 not on 52.172.33.23. Also, curiously, I don't have log/production.log file.
Are there any suggestions about this situation, or documents that I can read? Thanks
Check out proxy server in nginx documentation.
You can configure your nginx file like this as a start:
upstream backend {
server 127.0.0.1:3000;
}
server {
listen 80 default_server;
passenger_enabled on; # not sure about passenger, can try commenting out if it does not work
# root /home/ec2-user/my_app/public;
location / {
proxy_pass http://backend;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
This is the configuration on my project. Hope this works for your case.
By the way, I think here is a more appropriate place to ask nginx related question.

Resources