NGINX ignoring root when using proxy pass - nginx

I'm having a bit of a problem with NGINX. I made a simple HTML that loads my application in a iframe hosted on http://localhost:3000.
Using the config below, localhost:3000 is proxied but root /var/www is ignored so I only get to see the application without any of my html.
What would be the correct way to proxy a url loaded inside an iframe?
server {
listen 80;
listen [::]:80;
root /var/www/sub.mydomain.com/html;
index index.html index.htm index.nginx-debian.html;
server_name sub.mydomain.com;
location / {
proxy_set_header Host $host;
proxy_pass http://localhost:5601/;
}
}

Related

How to proxy to another URL without redirect

Is it possible to use nginx to work like this:
When going to x.com/blog -> display the content of blog.x.com (with all the query parameters, etc) without changing the browser URL (without browser redirecting)
I tried
location /blog {
proxy_pass https://blog.x.com
}
but that didn't work
This nginx config works for me.
server {
listen 80;
listen [::]:80;
server_name x.com;
location /blog {
proxy_pass https://blog.x.com;
proxy_set_header Host blog.x.com;
}
}

Nginx - Redirect domain to localhost:port content

I installed Nginx on my server (my server uses WHM). And on this server has two accounts. Each account will run a server a NextJS site and each account has its own domain.
Site1 will run on port 3000
Site2 will run on port 3004
What I want to do is:
I want to access domain1 I see the content of my site1 in NextJS that runs on localhost:3000
And when I access domain2 I see the content of my site2 on NextJS running on localhost:3004
I tried to do a Nginx implementation for site1. But when I accessed it I saw a Cpanel screen, and the url was dominio1/cgi-sys/defaultwebpage.cgi
Here's the Nginx implementation I tried to do:
server {
listen 80;
server_name computadorsolidario.tec.br www.computadorsolidario.tec.br ;
location / {
proxy_pass http://localhost:3004;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
}
}
So how do I do this setting for nginx to have this behavior? And I'm changing the correct file?
Note: I created the configuration file in /etc/nginx/conf.d/users/domain1/domio1.conf And within /etc/nginx/conf.d/users have several configuration files with the name of the accounts you have on the server. (They are already implemented.)
Try
server {
listen 80;
server_name www.domain1.com;
proxy_pass http://127.0.0.1:3000;
}
server {
listen 80;
server_name www.domain2.com domain2.com;
proxy_pass http://127.0.0.1:3004;
}
Each domain listens on same port and reverse-proxies to local network on the ports you specify. To differentiate between hosts, specify the server_name field.
server {
listen 80;
server_name www.domain1.com;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
server {
listen 80;
server_name www.domain2.com domain2.com;
location / {
proxy_pass http://127.0.0.1:3004;
}
}

simple Nginx configuration on t2 (AWS EC2) server gives 504 Gateway Time-out on subdirectories

I have a t2 server (AWS EC2 server) that I want to simply serve an HTML file as the root:
http://my_ip/ -> gives me my index.html
and another one serving a reverse_proxy
http://my_ip/api -> gives me my "API" (or whatever runs on the reverse proxy IP)
So I configured the Nginx as the followings:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location /api {
proxy_pass http://127.0.0.1:3000;
}
}
Simple enough, in this configuration, my index.html is showing up. But whenever I'm trying to access the "reverse proxy" (/api), I get 504 Gateway Time-out error.
So I tried to "switch places", I gave the reverse-proxy the "root" / and the index.html the "/api".
Getting this configuration:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_pass http://127.0.0.1:3000;
}
location /api {
try_files $uri $uri/ =404;
}
}
This way, the reverse proxy is doing OK! (from /), and when I try to reach index.html (from /api), I'm getting 504 Gateway Time-out error.
So I figured that something is wrong with the "sub-directories" configuration. I tried such configurations on many servers before (mainly from DigitalOcean and others) and never ran into something like that.
My guess is it got something to do with the t2 (EC2) NAT configuration (maybe?). But I'm not so sure.
Googling the issue didn't raise any answers.
There's Ubunutu 20.04 installed on the t2 server, It's "brand new" (nothing was installed but Nginx, Node & NPM - was done by me, "by the book" installations with no needed configurations or file changing).
Note that I don't have a domain yet, I was thinking of trying to server the reverse proxy on a subdomain but I have no way to do it at this point, that's why I tried to do it via a subdirectory.
Thanks.

Nginx sub-path shows 404 error

I am trying to setup nginx to work with a subpath but I am receiving 404 error.I am using default location and html file.
Nginx config is the following:
server {
listen 80;
listen[::]:80 ipv6only=on;
server_name localhost;
## serving gogs
location / {
proxy_pass http://localhost:3000;
}
## serving laravel-based web app
location /yt/ {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
The firewall (ufw) is disabled and all ports are open via mikrotik.Everything is fine when open the / path but when I go to /yt/ I get a 404 error.
Not sure if configuration is correct, when i compared with my local simple setup i found it little different . Please try this one if it works.
server {
listen 80;
listen[::]:80 ipv6only=on;
server_name localhost;
## serving gogs
location /yt/ {
proxy_pass http://localhost:3000/;
proxy_redirect off;
proxy_set_header Host $host;
}
}

Reverse proxy a URI to a Vagrant box

I have a Wordpress Vagrant box that I access through the URL localhost:9001 (forwarded port.).
I am currently trying to make it accessible through the URL "molecare.dev".
I have created the line in the hosts file that catches that URL and points it to my localhost (this is working because I can see the nGinx splash page) but I am having trouble catching this URL in the server block and proxy_pass'ing this to the URL(localhost:9001).
Here is my /etc/nginx/sites-available/default file`server_name molecare.dev;
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
I have written the following code which I think should do it but I don't know where to place it and if it is correct?
server {
listen 80;
server_name molecare.dev;
location / {
proxy_pass localhost:9001;
}
}
Can anyone see if this is correct and if so where I put this?
Thanks!
In the folder /etc/nginx/sites-available/ create a config file for your site, say 'molecare.dev.conf'.
Modify the block you wrote to the following and put that in the new file and save it:
server {
listen 80;
server_name molecare.dev;
location / {
proxy_pass http://127.0.0.1:9001;
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;
}
}
Then create a symlink:
ln -s /etc/nginx/sites-available/molecare.dev.conf /etc/nginx/sites-enabled/molecare.dev.conf
Reload the nginx configuration:
service nginx reload

Resources