Nginx proxy with Docker containers - nginx

This is my docker infra.
nginx webserver serving on port 80 (exposed to host)
media and blog serving on port 80 (not exposed to host) running under individual container
All running on the same VM.
Name Command State Ports
----------------------------------------------------------------------------------
media docker-php-entrypoint apac ... Up 80/tcp
mysql_db_blog docker-entrypoint.sh mysqld Up 3306/tcp
mysql_db_media docker-entrypoint.sh mysqld Up 3306/tcp
webserver nginx -g daemon off; Up 0.0.0.0:80->80/tcp
blog docker-entrypoint.sh apach ... Up 80/tcp
My nginx config:
server {
listen 80 default_server;
server_name 192.168.0.7;
server_tokens off;
location /story/ {
proxy_pass http://blog/;
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_set_header X-Forwarded-Host $server_name;
}
location /videos/ {
proxy_pass http://media/;
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_set_header X-Forwarded-Host $server_name;
}
}
My issue:
When I try to access any of the below URLs
1) http://webserver/story
2) http://webserver/videos
after the first request, the last part 'story' or 'videos' gets deleted form the url.
What am I missing from nginx conf to make sure 'story' or 'videos' doesn't gets removed from the url?

It's to do with how proxy_pass is written in your stanza.
From proxy_pass:
A request URI is passed to the server as follows:
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:
location /name/ {
proxy_pass http://127.0.0.1/remote/;
}
If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:
location /some/path/ {
proxy_pass http://127.0.0.1;
}
So change your proxy_pass:
location /videos/ {
proxy_pass http://media;
Note - lack of trailing slash, means it preserves the original URI.
You might also want to look at rewrite as that does something related.

Related

Run 3 websites on one domain using nginx

Lets say i have domain abc.com which redirects my server ip 1.2.3.4
i have 3 websites running on my server using docker container,
1st website ip 1.2.3.4:50/a
2nd website ip 1.2.3.4:60/b
3rd website ip 1.2.3.4:70/c
i can access this all websites using ip
i want to use my domain to run these three websites.
e.g.
abc.com/a should run 1st website
abc.com/b should run 2nd website
abc.com/c should run 3rd website
i am using nginx server version 1.14.1 on RHEL
Please help
i tried these configuration file
location /a {
rewrite ^/a(.*)$ http://1.2.3.4:50/a redirect;
}
location /b {
rewrite ^/b(.*)$ http://1.2.3.4:60/b redirect;
}
but it is redirecting and displaying the port in the url
Try this to solve your problem
location /a {
proxy_pass http://1.2.3.4:50;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /b {
proxy_pass http://1.2.3.4:60;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /c {
proxy_pass http://1.2.3.4:70;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
For your information, this proxy configuration will make your abc.com/a to access the root of your http://1.2.3.4:50 website. But the /a will also be passed to the http://1.2.3.4:50 so you don't have to bother add http://1.2.3.4:50/a for each proxy
You can configure it explicitly inside the nginx.conf
// Nginx.conf
http {
...
server {
server_name abc.com;
// here is location ...
}
}
Or by including configurations in sites-available, usually in the nginx.conf you can found this section:
// Nginx.conf
http {
...
include /etc/nginx/sites-available/*;
...
}
Then you can add site.conf or main.conf, you name it yourself for example in sites-available folder that has this kind of configuration:
server {
server_name abc.com;
// here is location ...
}

nginx reverse proxy for application

I use nginx for reverse proxy with domain name. I've some application publish on IIS and i want to proxy different location name for each application.
For example;
Domain name on nginx :
example.com.tr
application end points for app:
1.1.1.1:10
1.1.1.2:10
upstream for app in nginx.conf:
upstream app_1 {
least_conn;
server 1.1.1.1:10;
server 1.1.1.2:10;
}
server {
listen 443 ssl;
server_name example.com.tr;
proxy_set_header X-Forwarded-Port 443;
ssl_certificate /etc/cert.crt;
ssl_certificate_key /etc/cert.key;
location /app_1/ {
proxy_pass http://app_1/;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-REAL-SCHEME $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
access_log /etc/nginx/log/access.log;
error_log /etc/nginx/log/error.log;
}
}
When I try to access example.com.tr/app_1/ , I can access application but not all data.
I inspected this site and so many requests of application were failed.
All requests sended to example.com.tr/uri instead of example.com.tr/app_1/uri. How can I fix this ?
thanks,
You need a transparent path proxy setup. Means NGINX should use the requested URI without removing the matched location from it.
proxy_pass http://app_1;
Remove the tailing slash to tell NGINX not to do so. Using an upstream definition is great but make sure you apply keepalive.

Nginx redirect subfolder to another port showing original port

How to enable Nginx redirect subfoler to another port using original port? e.g.,
Service1: http://127.0.0.1:5000
Service2: http://127.0.0.1:8080
Exposed IP port via Nginx is 127.0.0.1:6060
The goal is when accessing http://127.0.0.1:6060/sub, it will access http://127.0.0.1:8080, but the URL user see is still http://127.0.0.1:6060/sub.
I tried two configurations, but they didn't work.
server {
listen 6060;
server_name 127.0.0.1;
location /sub/ {
# method 1: use proxy pass, browser says "static resources not found"
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host:6060;
proxy_pass http://127.0.0.1:8080;
# method 2: use rewrite, the URL will be http://127.0.0.1:8080
rewrite ^/pct/(.*)$ http://127.0.0.1:8080 redirect;
}
location / {
proxy_pass http://127.0.0.1:5000;
}
Thanks.

nginx reverse proxy remove subpath on upstream

I have an api server I am reverse proxying with nginx. It is functionally working but I want to change the current behavior.
api server url:
http://apiserver:5000/api/v1/ping
the above becomes accessible by this nginx url (see the double 'api' part?):
https://nginxserver/api/api/v1/ping
How can I write the config so that /api hits the api server but without adding an additional 'api' to the nginx url.
location ^~ /api {
proxy_pass http://apiserver:5000/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Script-Name /api;
}

Nginx reverse proxy setting

I am totally new to Nginx and need your help.
Basically I have a single server with single IP address, but I want to host two different web application within the server with different domain name. So, basically, for each domain name, I want it to redirect to different port number. I tried below and got an error
[root#mysvr nginx]# nginx -t -c /etc/nginx/nginx.conf
nginx: [emerg] "proxy_pass" directive is not allowed here in /etc/nginx/nginx.conf:41
nginx: configuration file /etc/nginx/nginx.conf test failed
Following is the Nginx setting. Line 41 is where the proxy_pass is.
server {
listen 80;
server_name server1.com www.server1.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:1003;
}
server {
listen 80;
server_name server2.com www.server2.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.1.1:1004;
}
Thank you!
If you check the docs for proxy_pass, proxy_pass needs to be in a location, if in location or limit_except block. You have it in a server block.
Try replacing your usage of proxy_pass with
location / {
proxy_pass ...
}

Resources