nginx reverse proxy all domains - nginx

i'm trying to reverse proxy any requested domain, my code works for specific domains only, for example:
server {
listen 80;
server_name localhost;
location / {
rewrite_log on;
proxy_pass https://www.example.com;
}
}
this didn't work when http://localhost:80/www.example.com requested.
location ~ ^/(.*)/ {
resolver 8.8.8.8;
proxy_pass http://$1;
}
nor this one
location / {
resolver 8.8.8.8;
proxy_pass http://$http_host$uri$is_args$args;
}

this actually works, i didn't realize that because HTTPS websites doesn't work on it.
since HTTPS is the standard nowadays on almost any website, this may not worth it for everyone.
server {
listen 80;
resolver 8.8.8.8;
location / {
proxy_pass http://$http_host;
proxy_set_header Host $http_host;
}
}
i couldn't find a solution for the HTTPS problem, it would be great to have that.

Related

Nginx reverse proxy to frontend and backend

I have two react app running on localhost:3000(frontend) and localhost:3001(backend). I want to serve both backend and front end from same server_name.. For example, if a user hits example.com the Nginx should route the traffic to frontend running on (localhost:3000) and if a user hits example.com/admin/login traffic should get routed to the backend (localhost:3001).
'''
server {
listen 80;
server_name example.com;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
proxy_pass http://localhost:3001;
}
location /admin-login {
proxy_pass http://localhost:3000;
}
}
'''
Using above configuration. I have frontend running on example.com. However, when I call example.com/admin/login I am getting redirected to app running on frontend (localhost:3000) instead of backend running on (localhost:3001).
Updating as per the answer given below. I have below configuration. it still have the same behavior.
server {
listen 80;
server_name example.com;
location /admin-login {
proxy_pass http://127.0.0.1:3000/admin-login;
}
location / {
proxy_pass http://127.0.0.1:3001;
}
location /home {
proxy_pass http://127.0.0.1:3001/home;
}
location /login {
proxy_pass http://127.0.0.1:3001/login;
}
location /signup {
proxy_pass http://127.0.0.1:3001/signup;
}
location /article {
proxy_pass http://127.0.0.1:3001/article;
}
}
I think the problem here is that
location / {
proxy_pass http://localhost:300
}
matches all queries so will also redirect anything to /admin-login
You could either rearrange your blocks to have the admin-login block above the / block in the config file, or make the adjustment below:
location = / {
proxy_pass http://localhost:300
}
This adjusted block should only redirect queries to / rather than /*
If you want to read more, it's explained in the documentation here - https://nginx.org/en/docs/http/ngx_http_core_module.html#location

Two Play deployments servers on the one physical server

I want to have two Play Framework deployments on the same server and configure nginx to redirect the locations accordingly.
I have this configuration:
server {
listen 80;
server_name localhost;
client_max_body_size 20M;
location /site2 {
proxy_pass http://localhost:8000/;
}
location / {
proxy_pass http://localhost:9000/;
}
}
But it does not work.
What I want is:
When I use: http://ip/ or http://ip/something the Play deployment at port 9000 should respond.
When I use: http://ip/site2/ or http://ip/site2/something the Play deployment at port 8000 should respond.
First, please check that you have access to each app directly:
ip:8000
ip:9000
Next please add proxy_redirect directive:
server {
listen 80;
server_name localhost;
location /site2 {
proxy_pass http://localhost:8000/;
proxy_redirect http://localhost/site2/ http://localhost:8000/;
}
location / {
proxy_pass http://localhost:9000/;
proxy_redirect http://localhost/ http://localhost:9000/;
}
}

Kibana dashboard couldn't connect with Nginx

Hi i'm trying to use Nginx as a reverse proxy for accessing a Kibana 4 dashboard. The location of the dashboard is not available in the latest kibana but it can be accessed using a URL.
Kibana and Nginx are running both locally and installed on a windows machine installed in C:\
Kibana is running on localhost:5601.
I installed NGinx and configured it to run on port 80. My config file of Nginx looks like this.
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name 127.0.0.1:5601;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ {
proxy_pass http://127.0.0.1:5601;
#proxy_redirect https://kibana/;
}
}
But when i enter localhost in my browser i see,
"Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx."
Kibana is working fine with : localhost:5601.
Do i need to make any changes to the Kibana config file also? I want to access the kibana dashboard by localhost:80 through NGinx.
Thanks
Change "server_name 127.0.0.1:5601;" to "server_name localhost:80;"
Add this upstream above "server {" :
upstream kibana {
server localhost:5601;
}
and then replace "location ~" with :
location /kibana/ {
proxy_pass http://kibana/;
}
Use http://localhost/kibana to access Kibana
I have configured my nginx to reverse proxy the kibana-4 dashboard. The following nginx config does the job for me:
server {
listen 80;
#You can add your fqdn, say example.com, if you want to in the next parameter
server_name localhost;
auth_basic off;
location / {
proxy_pass http://localhost:5601;
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;
}
}
here is how you can proxy to kibana through nginx kibana and ES on a remote server with https using letencrypt
server {
listen [some_port] ssl http2;
server_name [server_name];
root /your/root/directoty;
location /app {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/conf.d/yyyyyyyyy.passwd;
proxy_pass http://example.com:5601;
}
location /bundles {
proxy_pass http://example.com:5601/bundles;
}
location /elasticsearch {
proxy_pass [http://elasticsearch_server:9200;]
}
location /status {
proxy_pass http://example.com:5601/status;
}
location /api {
proxy_pass http://example.com:5601/api;
}
location /plugins {
proxy_pass http://example.com:5601/plugins;
}
location /ui {
proxy_pass http://example.com:5601/ui;
}
location /es_admin {
proxy_pass http://example.com:5601/es_admin;
}
location /built_assets {
proxy_pass http://example.com:5601/built_assets;
}
location /node_modules {
proxy_pass http://example.com:5601/node_modules;
}
location /translations {
proxy_pass http://example.com:5601/translations;
}
location /internal {
proxy_pass http://example.com:5601/internal;
}
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/yourdomain.com/chain.pem;
include snippets/ssl.conf;
include snippets/letsencrypt.conf;
access_log /var/log/nginx/xxxx.access.log;
error_log /var/log/nginx/xxxxx.error.log;
passenger_enabled on;
passenger_min_instances 1;
client_max_body_size 10m;
}

nginx and monit web interface - how to have monit on domain.com/monit versus domain.com

I am using monit on my ec2 instance and I am new to nginx. Below is my nginx config file:
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass 127.0.0.1:2812;
proxy_set_header Host $host;
}
}
So..if I go to domain.com I see monit. How do I modify above code where I can see monit on domain.com/monit?
Thanks
Please, try this:
server {
listen 80;
server_name 127.0.0.1;
location /monit/ {
proxy_pass http://127.0.0.1:2812;
proxy_set_header Host $host;
}
}
Please, read more here about how directive location works in nginx
There's an article in Monit's wiki how to configure it with Nginx.
Here's my /etc/nginx/conf.d/monit.conf:
server {
listen 80;
server_name my.server.name;
location /monit/ {
allow 127.0.0.1;
allow 192.0.0.0/8;
deny all;
proxy_pass http://127.0.0.1:2812;
proxy_set_header Host $host;
rewrite ^/monit/(.*) /$1 break;
proxy_ignore_client_abort on;
}
}
Sergei already correctly answered your direct question. I think it's also worth noting that this may be cleaner to just use a subdomain:
server {
listen 80;
server_name monit.domain.com;
location / {
proxy_pass 127.0.0.1:2812;
proxy_set_header Host $host;
}
}

Multiple apps on nginx

I'm trying to route traffic across multiple upstream servers on nginx like so:
upstream app_a {
server unix:/tmp/app_a.sock fail_timeout=10;
# For a TCP configuration:
# server localhost:8000 fail_timeout=0;
}
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
index index.html index.htm;
server_name localhost;
root /home/ubuntu/app_a/www/staging/static;
location ~ ^/app_a/(.*)$ {
try_files $1 #proxy_to_app_a;
}
location #proxy_to_app_a {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_a;
}
Unfortunately the apps have no knowledge of full uris and expect to be sitting on root - which means i need to re-write the uri when passing to the app, which is why i thought this might work:
location ~ ^/app_a/(.*)$ {
try_files $1 #proxy_to_app_a;
}
the app works fine if location is just / (because of the aforementioned root issue), but this regex based solution doesnt seem to work. What do i need to do so the app gets / instead of app_a in the url?
Thanks
location /app_a/ {
rewrite /app_a/(.*) /$1 break;
proxy_set_header Host $http_host;
proxy_pass http://app_a;
}

Resources