Map docker containers ports with nginx - docker

When a location is with root and another is with proxy_pass nginx does not work in the url /laravel. The response of this url is "404 Not Found".If I remove url location / and /moda, the url /laravel works. I do this, because I want map docker containers.
nginx.conf file :
server {
listen 80;
server_name local.monllar.com;
location /laravel {
root /var/www/local.monllar.com/public_html;
index index.html index.htm;
}
location / {
proxy_pass http://localhost:32768;
}
location /moda {
proxy_pass http://localhost:2222/moda;
}
}

I found the solution. This maps the ips of the docker containers to my local server names
nginx.conf file :
server
{
listen 80;
server_name local.monllar.com;
location / {
root /var/www/local.monllar.com/public_html;
index index.html index.htm;
}
}
server
{
listen 80;
server_name local.moda.com;
location / {
proxy_pass http://localhost:2222/moda/;
}
}
server
{
listen 80;
server_name local.laravel.com;
location / {
proxy_pass http://localhost:32768;
}
}
/private/etc/hosts file in Mac
127.0.0.1 local.monllar.com
127.0.0.1 local.moda.com
127.0.0.1 local.laravel.com

Related

How to Reverse Proxy using Nginx to Dockerized Swagger UI's along with web apis?

I have 2 servers, one with dockerized nginx and one with 3 dockerized web apis allowing traffic through different ports (say 441, 442, 443) which has swagger UI along with it respectively.
with limited knowledge on nginx, I am trying to reverse proxy to all the swagger UI endpoints using the nginx container. This is how my nginx conf looks like, but it doesnt work as expected, it would be great if someone can advice where I am going wrong.
I am able to hit the service with the exact match location context /FileService which return index.html. But index.html has the script call where nginx fails to serve these static contents.
index.html
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
nginx.conf
server {
listen 443 ssl http2;
server_name www.webby.com;
access_log /var/log/nginx/access.log;
ssl_certificate /etc/ssl/yyyy.crt;
ssl_certificate_key /etc/ssl/xxxx.key;
ssl_protocols TLSv1.2;
if ($http_referer = 'https://$host/FileService') {
rewrite ^/(\w+) /swagger/fileservice/$1;
}
if ($http_referer = 'https://$host/PreProcess') {
rewrite ^/(\w+) /swagger/preprocess/$1;
}
location = /FileService {
proxy_pass 'http://appy.com:441/swagger/index.html';
}
location = /PreProcess {
proxy_pass 'http://appy.com:442/swagger/index.html';
}
# curl http://appy.com:441/swagger/swagger-ui-bundle.js is giving the js on this container
location ~* /swagger/fileservice(.*) {
proxy_pass 'http://appy.com:441/swagger/$1';
}
location ~* /swagger/preprocess(.*) {
proxy_pass 'http://appy.com:442/swagger/$1';
}
}
accesslog on the nginx looks like
anyways I struggled my way to implement this. Not sure if its the right approach (because I read on the internet that if block inside location context is evil), but works for my case. Feel free to correct my answer
server {
listen 443 ssl http2;
server_name www.webby.com;
access_log /var/log/nginx/access.log;
ssl_certificate /etc/ssl/yyyy.crt;
ssl_certificate_key /etc/ssl/xxxx.key;
ssl_protocols TLSv1.2;
location = /FileService {
proxy_pass 'http://appy.com:441/swagger/index.html';
}
location = /PreProcess {
proxy_pass 'http://appy.com:442/swagger/index.html';
}
location ~ ^/swagger/(.*)$ {
if ($http_referer = 'https://$host/FileService') {
proxy_pass 'http://appy.com:441/swagger/$1';
}
if ($http_referer = 'https://$host/PreProcess') {
proxy_pass 'http://appy.com:442/swagger/$1';
}
}
location ~ ^/swagger(.*)$ {
if ($http_referer = 'https://$host/FileService') {
proxy_pass 'http://appy.com:441/swagger/swagger$1';
}
if ($http_referer = 'https://$host/PreProcess') {
proxy_pass 'hhttp://appy.com:442/swagger/swagger$1';
}
}
}

Reverse proxy of multiple container

I have 2 API containers (docker) running on port 10000 and 10003. I want to reverse proxy both of them so the API can be called from a single port which is port 80. I am trying to use NGINX to do that and this is my nginx configuration file:
worker_processes 1;
events { worker_connections 1024; }
http {
server {
listen 80;
server_name container1;
location / {
proxy_pass http://10.10.10.50:10003;
}
}
server {
listen 80;
server_name container2;
location / {
proxy_pass http://10.10.10.50:10000;
}
}
}
I found that it is only working on the container 1 and if there is a request for container 2, it will generate 404 not found warning because the request go to the container 1 instead of container 2.
Finally, I found a solution using NGINX. All I need to do is to create a new NGINX container then reconfigure the url of my 2 API container. The configuration file that I wrote looks like this:
worker_processes auto;
events { worker_connections 1024; }
http {
upstream container1 {
server 10.10.10.50:10003;
}
upstream container2 {
server 10.10.10.50:10000;
}
server {
listen 80;
location /container1/ {
proxy_pass http://container1/;
}
location /container2/ {
proxy_pass http://container2/;
}
}
}
Now, I can make requests for both API containers by using port 80 as it will be re-routed from the port into the designated port (reverse-proxy).

How use nginx to create subdomains and map API to localhost:80?

I have a docker that run my api on localhost:80 and 2 folder for my fronts.
Here is what I want:
If I visit api.example.com map it to localhost:80
If I visit admin.example.com map it to folder ~/admin
If i visit example.com map it to folder ~/front
How can do this?
I found my answer and thanks from #Patrick-Mevzek.
I solved my problem by adding below server blocks to my nginx configuration.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/erfantahvieh.com/front;
server_name domain.example www.domain.example;
index index.html index.htm index.nginx-debian.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
autoindex on;
autoindex_exact_size off;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
root /home/erfantahvieh.com/admin;
server_name admin.domain.example www.admin.domain.example;
index index.html index.htm index.nginx-debian.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
root /usr/share/nginx/html;
server_name api.domain.example www.api.domain.example;
index index.html index.htm index.nginx-debian.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://xxx.xxx.xxx.xxx:8080/;
}
}

Rails app on two domain + nginx + passenger

How can I have one app on two domains using nginx + passenger? I add second domain in nginx but this redirect to domain1.net
server {
listen 80;
server_name domain2.net *.domain2.net;
rewrite ^/(.*)$ http://www.domain2.net/$1 permanent;
}
server {
listen 80;
server_name www.domain2.net;
root /var/www/domain1/public/; # <--- be sure to point to 'public'!
}
server {
listen 80;
server_name domain1.net *.domain1.net;
rewrite ^/(.*)$ http://www.domain1.net/$1 permanent;
}
server {
listen 80;
server_name www.domain1.net;
root /var/www/domain1/public/; # <--- be sure to point to 'public'!
passenger_enabled on;
client_max_body_size 20M;
location / {
passenger_enabled on;
root /var/www/domain1/public/;
}
}
What I want is to have one app on two domains because I want to translate app for users from second domain.
server {
listen 80;
server_name domain2.net *.domain2.net;
rewrite ^/(.*)$ http://www.domain2.net/$1 permanent;
}
server {
listen 80;
server_name www.domain2.net;
root /var/www/domain1/public/;
passenger_enabled on;
location / {
passenger_enabled on;
root /var/www/domain1/public/;
}
}
server {
listen 80;
server_name domain1.net *.domain1.net;
rewrite ^/(.*)$ http://www.domain1.net/$1 permanent;
}
server {
listen 80;
server_name www.domain1.net;
root /var/www/domain1/public/; # <--- be sure to point to 'public'!
passenger_enabled on;
client_max_body_size 20M;
location / {
passenger_enabled on;
root /var/www/domain1/public/;
}
}
you should add passenger stream also to the second domain then it should work.
One tip move each domain into site.conf file then include then in nginx.conf this will be easier to maintenance.
make sure you restart Nginx after the change.

Deploing Rails App on other Port

simple question, How can I deploy my Rails Application into a port of my website? I know i can specify the port when running using Mongrel or Webrick, but this time, I have to deploy it into production. I think passenger can manage this but I dont know how. I tried search but still I can't find the solution. Please help :)
Thanks!
Follow-up:
I am using Ubuntu 10.04 LTS and my Passenger runs with Apache.
If you're using Passenger with Apache or nginx. It will use the default port, 80. You can change this in the config file based on which web server you use.
# HTTPS server
server {
listen 80;
listen 8080;
server_name *.host.com;
root /home/app/public_html/host_production/current/public;
error_page 500 502 504 /500.html;
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
error_page 503 #503;
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
try_files $uri /system/maintenance.html #passenger;
location #passenger {
passenger_enabled on;
passenger_min_instances 5;
rails_env production;
}
if ($request_method !~ ^(GET|HEAD|PUT|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location ~ \.php$ {
deny all;
}
access_log /dev/null;
error_log /dev/null;
}
nginx+passenger config

Resources