trying to log ip via nginx reverse proxy - nginx

I have Nginx serving a node.js and react application that communicate with each other. I deployed to my cloud with pm2.
I am storing IPs that are connecting and instead of the client's IP, I get the client fwd'd proxy ::ffff:127.0.0.1
My nginx conf:
upstream loadbalancer {
least_conn;
server 127.0.0.1:3500;
server 127.0.0.1:3501;
}
server {
index index.html index.htm index.nginx-debian.html;
server_name site.co www.site.co;
listen 80;
listen [::]:80;
# react app & front-end files
location / {
root /var/www/html/client/build;
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://loadbalancer/;
proxy_buffering on;
}
}
What am I missing here?

Related

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;
}
}

nginx Reverse Proxy with 2 servers

So i've came across a cool project and i wanted to recreate it. It is my first time using nginx and also my first time learning things about a reverse proxy. I've currently have a reverse proxy running and it works (I guess). But the Proxy currently only works with other ports. I have 3 servers that are running nginx. I use one of them as my reverse proxy. I can access the other servers with different ports. See here (reverse-proxy.conf):
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
proxy_pass http://192.168.2.20;
}
}
server {
listen 8080;
root /var/www/html;
server_name localhost;
location / {
proxy_pass http://192.168.2.30;
}
}
Are there a way to use the reverse proxy without using different ports? Or is my solution ok? At the end i just need a reverse proxy that is able to communicate with 2 other servers.
So one thing here people use reverse proxy in a different ways
But most generic usecase is redirect using location.
Please find the below example.
server {
listen 80;
root /var/www/html;
server_name localhost;
location /a {
proxy_pass http://192.168.2.20;
}
location /b {
proxy_pass http://192.168.3.20;
}
}
Another is giving weight to each proxy.
Please find the below example
stream {
upstream stream_backend {
server http://192.168.2.20 weight=75;
server http://192.168.3.20 weight=25;
}
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
proxy_pass stream_backend;
}
}
In above 192.168.2.20 will receive 75% of the load and 192.168.3.20 will receive 25% of the load. In case if you want to distribute the equal load to both(or round-robin method) Please remove the weight.
I think you may not understand how Nginx work about proxy.
Nginx can reverse Proxy L7 http or L4 stream
and you set the proxy listen on any port or URL you want and proxy to any server or port or URL you want.
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
proxy_pass http://192.168.2.20:2323/URL;
}
}
server {
listen 8080;
root /var/www/html;
server_name localhost;
location / {
proxy_pass unix:/tmp/backend.socket;
}
}
Here is a reference for you about the proxy_pass directive.
proxy_pass

EC2 - can ssh but cannot access to a web page server by Nginx

I setup my ECS instance with my own VPC, with its own Security Group (:22/:80/:443 open).
Gateway everything is working.
I can ssh to my server with the public IP.
But I cannot serve a simple HTML page with Nginx. I use the config I had on my own server but I cannot reach my page.
Nothing fancy in my Nginx config :
server {
listen 80;
listen [::]:80;
server_name www.gfelot.xyz gfelot.xyz;
root /var/www/siteWeb;
index index.html;
location / {
try_files $uri $uri/ =404;
}
# location ~ /.well-known {
# allow all;
# }
}
Any idea why ?
I suggest you check firewall rules and SELinux setting on VPC server.

Nginx subdomains not working

I don't know what's wrong. I don't get any warnings in logs. I've similar config to this
How to exclude specific subdomains server_name in nginx configuration
I want to create subdomain us.example.io I'm using ping to check it
ping us.example.io
ping: cannot resolve us.example.io: Unknown host
nginx.config
server {
server_name *. us.example.io us.example.io;
listen 80;
root /usr/share/nginx/html/bint;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
server_name us.example.io;
listen 80;
return http://www.google.com;
}
The problem has nothing to do with nginx. The error suggests that you haven't configured a DNS record for the domain.

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;
}
}

Resources