nginx direct trafic to non domain name using IP and local names - nginx

Using nginx on windows server I want to direct traffic into different ports using names that are non-domain names, first one works but second one never reach: why? what is wrong?
http://192.xxx.xxx.xxx/game: works
http://192.xxx.xxx.xxx/cms: never reach.
If I change names then cms works and game never reach.
server {
listen 80;
server_name game;
location /{
proxy_pass http://localhost:4040;
proxy_connect_timeout 60s;
proxy_read_timeout 5400s;
proxy_send_timeout 5400s;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect default;
}
location /uploads/ {
root c:\Ebrahimi\www\appGame;
}
}
server {
listen 80;
server_name cms;
location /{
proxy_pass http://localhost:2010;
proxy_connect_timeout 60s;
proxy_read_timeout 5400s;
proxy_send_timeout 5400s;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect default;
}
location /uploads/ {
root c:\Ebrahimi\www\appCms;
}
}
New code block as "Richard Smith" mentioned:
server {
listen 80;
location /{
proxy_pass http://localhost:4040;
proxy_connect_timeout 60s;
proxy_read_timeout 5400s;
proxy_send_timeout 5400s;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect default;
}
location /uploads/ {
root c:\Ebrahimi\www\appGame;
}
location /game {
proxy_pass http://localhost:4040;
}
location /cms{
proxy_pass http://localhost:2010;
}
}

The second server block can only be accessed using its server_name, for example: http://app.firouzeh-mfg.ir/
If you are accessing your server using its IP address, the request will be processed by the first server block (or the block marked as the default_server). See this document for details.
Your two URLs http://192.x.x.x/game and http://192.x.x.x/cms (assuming identical IP addresses) access the same server block and differ only by location.
For example:
server {
location /game {
proxy_pass http://localhost:4040;
}
location /cms{
proxy_pass http://localhost:2010;
}
}

Related

How to stop Nginx redirect if HOST HEADER is incorrect

I have been trying to solve this issue for quite awhile now. Bots are hitting my sites hard with INVALID HOST HEADERS and Nginx forwards these requests to Gunicorn/Django. I need to stop them at Nginx. I have tried every solution I can find on SO, and elsewhere, but none seem to work for my setup.
Nginx.conf:
upstream backend_server {
server backend:8000;
}
upstream backend_asgi {
server backend_asgi:8001;
}
server {
listen 80;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location ~* ^/(api|admin|static|v2) {
return 301 https://$host$request_uri;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.site *.example.site;
ssl_certificate /etc/letsencrypt/live/example.site/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.site/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location /ws/ {
proxy_pass http://backend_asgi;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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;
proxy_set_header X-Forwarded-Host $server_name;
}
location ~ ^/v2(?:/(.*))?$ {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /v2/index.html =404;
}
location /backend_static/ {
alias /backend/assets/;
}
location /media/ {
alias /backend/media/;
}
location ~* ^/(api|admin) {
proxy_pass http://backend_server$request_uri;
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-Proto $https;
proxy_connect_timeout 360s;
proxy_read_timeout 360s;
}
location / {
proxy_pass http://backend_server$request_uri;
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-Proto $https;
proxy_connect_timeout 360s;
proxy_read_timeout 360s;
# Set upload size for videos to be 500MB
client_max_body_size 500M;
}
}
What can i add to my Nginx configuration to stop invalid host headers, given that I have a wildcard subdomain and bots are also using HOST HEADERS w/ subdomains?

Nginx returns 404 on proxy_pass

I have the following configuration on Nginx 1.20.1, whenever I try to access test.xxx.com/something/ I get a 404 error. I know there are other similar questions but I already have the / at the end of the proxy_pass so I have no idea what to do. The strange thing is that I have 20 other servers on that configuration, they are all identical but only this one doesn't work. Any idea?
upstream test {
server x.x.x.x:8443;
}
server {
listen 8090;
server_name test.xxx.com;
root /var/www/vhosts/test.xxx.com/;
location / {
access_log /var/log/nginx/access_test.log upstreamlog;
proxy_pass https://test/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 75s;
proxy_send_timeout 1800s;
proxy_read_timeout 1800s;
}
}

Nginx proxy: rewrite rule for root request

I have nginx installed on port 80 and a node application on port 2368 behind nginx
nginx configuration looks like this
server {
server_name domain.com www.domain.com;
listen 80;
location / {
proxy_pass http://localhost:2368;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
}
}
This configuration works exactly as expected. For example / request turns into http://localhost:2368/, and /post/mypost turns into http://localhost:1234/post/mypost etc.
What I want is that only / request turned into http://localhost:2368/latestpost/. And all other requests are handled the same way as in example above. Thnx!
You could use rewrite directive:
server {
server_name domain.com www.domain.com;
listen 80;
location / {
rewrite ^/$ /latestpost/ break;
proxy_pass http://localhost:2368;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
}
}
or in separate location:
server {
server_name domain.com www.domain.com;
listen 80;
location = / {
rewrite ^.* /latestpost/;
}
location / {
proxy_pass http://localhost:2368;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
}
}
Second variant is slightly more efficient as it will not try rewrite every request. But difference will be unnoticeable, I guess.

how to reverse proxy via nginx a specific url?

I have a server running at http://localhost:8080 i want a specific url of this server to be proxied by nginx.
For example, i only want http://localhost:8080/test/(.*) to be reverse proxied to http://localhost/test/(.*).
I'm proxing another server to http://localhost/.
What about just a simple location block?
server {
# ... other stuff
location /test/ {
try_files $uri #testproxy;
}
location #testproxy {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
# all your params
}
}
I made it somehow this way and it worked. Thanks for your comment anyway. :)
server {
listen 80;
# ... other stuff
upstream backend1 {
server 127.0.0.1:8080;
}
location /test/ {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://backend1/test/;
}
}

nginx url rewrite for reverse proxy

I have an nginx on port 80 and a tomcat on port 8080 configured as upstream.
The war application in tomcat listen to /pwm.
I would like to configure nginx to a reverse proxy for tomcat and rewrite the url "/" to "/pwm".
example:
user types "web.noc.local" in browser and nginx rewrites the url to web.noc.local/pwm and redirects to tomcat on port 8080.
my nginx config:
upstream pwm_server {
server 127.0.0.1:8080 fail_timeout=0;
}
server {
listen 80;
server_name web.noc.local;
access_log /var/log/nginx/log/web.noc.local.access.log main;
error_log /var/log/nginx/log/web.noc.local.error.log;
location / {
if ($is_args != "") {
rewrite "^$" /pwm break;
expires 7d;
proxy_pass http://pwm_server;
}
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_max_temp_file_size 0;
proxy_buffering off;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://pwm_server;
}
}
now when I open the url the, nothing happens, only a blank screen.
thx for help.
Ok, I found a solution for me:
location / {
rewrite ^ http://web.noc.local/pwm/ last;
}
location /pwm {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_max_temp_file_size 0;
proxy_buffering off;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://pwm_server;
}

Resources