How to redirect certain IP to localhost - nginx

I want to block certain IP, and replace the Response with Block Page I made by Flask Server in localhost.
How to config that in Nginx.
For Example:
The program 'A' uses requests.get("http://123.123.123.123/GetInfo")
I want to replace the http://123.123.123.123/GetInfo with HTTP://localhost:5000/GetInfo
Is that possible for Nginx?
I can't change the source code of Program 'A'.
And I don't know what others program in my local will access http://123.123.123.123/GetInfo
I've tried the following configuration:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 127.0.0.1;
location /GetInfo {
proxy_pass http://127.0.0.1:6000/GetInfo ;
}
}
server {
listen 123.123.123.123:5000;
server_name 123.123.123.123;
rewrite ^/(.*)$ http://127.0.0.1/$1 permanent;
}
}
It doesn't working.

Related

nginx reroute the passthrough request

On my server two types of services are running. some services are normal and need ssl certificate but one service should use pass through. I read the document and what I understood is that I need to create a stream on a new port. if I am using 443 for ssl then I can't use it for passthrough.so created a pass through stream on a new port 8443. Every thing works fine but for passthrough service I need to enter the port along with url e.g https://production-server:8443. I want it like
https://production-server:8443 -> https://production-server
so my question is can we reroute a request in nginx ? here is my configuration
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream unsecure{
server prod-exec:8040 ;
keepalive 64;
}
upstream secure {
server prod-exec:9001
keepalive 64;
}
server {
listen 80;
server_name ServerA;
access_log C:/nginx-1.20.1/logs/access.log upstreamlog;
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location / {
proxy_pass http://unsecure/services;
}
location /services/countingservice {
proxy_pass http://unsecure;
}
}
location /services/balancingservice {
proxy_pass http://unsecure;
}
}
server {
listen 443 ssl;
server_name server-A:443;
access_log C:/software/nginx-1.20.1/logs/access.log upstreamlog;
ssl_certificate C:\\nginx-1.20.1\\ssl\\certificate.crt;
ssl_certificate_key C:\\nginx-1.20.1\\ssl\\certificate_key.key;
error_page 401 /test_401.html;
location = /test_401.html {
root /C:/nginx-1.20.1/html;
internal;
}
location / {
proxy_pass https://secure/services;
}
location /services/countingservice {
proxy_pass http://secure;
}
}
location /services/balancingservice {
proxy_pass http://secure;
}
}
stream {
access_log C:/nginx-1.20.1/logs/access.log main;
upstream passthrough_test {
server prod-exec:9001 max_fails=2 fail_timeout=180s ;
# Definition of Nginx server (URL + Port) for Application 1
server {
listen server A: 84443;
listen 84443;
proxy_pass passthrough_test;
proxy_next_upstream on;
}
}
I don't want to add port with URL for pass through. so if I config the passthrough on 443 can nginx filter the request by recognizing it's pattern?
or is there any other way?
Any help would be appreciated. Thanks in Advance.

Nginx rewrite localhost to /usr/share/nginx/static

I want every request for http://localhost/static/ to be directed to /usr/share/nginx/static.
For example, it the user requests http://localhost/static/style/style.css, I want the server to reply with the content of /usr/share/nginx/static/style/style.css.
This is my code (which does not work):
server {
server_name http://localhost/;
rewrite ^/static/(.*) /usr/share/nginx/$1 permanent;
}
And this is the main config file:
worker_processes 1;
events {
worker_connections 200;
}
http {
# Basic Settings
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Logging Settings
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Include Other Configs
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Update: I have a file named mysite.conf like this which does not rewrite the urls as i mentioned in the question.
location /static {
root /usr/share/nginx/static;
}
Replace
server_name http://localhost/;
by
server_name localhost;
Per https://nginx.org/en/docs/http/ngx_http_core_module.html#server_name after the server_name directive you put the server's name, not an URL.
Also rewrite is to do HTTP redirections, so instead of
rewrite ^/static/(.*) /usr/share/nginx/$1 permanent;
use:
location /static {
root /usr/share/nginx;
}
See this other answer for another example: Use nginx to serve static files from subdirectories of a given directory

Passing a custom IP address with Nginx proxy server

My apologies if the title isn't descriptive, I wasn't sure how to word this. I need to connect to a VPN, and need a specific IP address to do it. I was wanting to use Nginx configured as proxy server to do this. Here is my Nginx config file:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 8080;
server_name name;
location / {
resolver 8.8.8.8;
proxy_pass http://$http_host$uri$is_args$args;
proxy_bind 10.0.0.13;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
I had thought I could achieve what I wanted with proxy_bind, but whenever I try to connect to a website I am given an Nginx error page. I checked the error log, and I get an error saying it couldn't assign the address. I don't even know if that's actually what I want to do. Any suggestions? Thanks in advance.

nginx issues 404 errors. The file can be access from the browser no problems. ununderstable.

I have a simple nginx setup. When i try to go to the localhost from the browser, i get a 404 error.
Here my nginx config file:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
ssi on;
location / {
index /srv/http/city_db/static/index.html;
ssi on;
}
}
}
If I simple type in the url-bar in the browser /srv/http/city_db/static/index.html
it instanteneously takes me to the file and all works normally.
And, also I did systemctl restart nginx ofcourse.
Can someone please tell me what I'm doing wrong?
Thanks in advance.
You need to set the root to /srv/http/city_db/static/ and index to index.html. For this configuration you do not need the location block, you can set both at the server level.
server {
listen 80;
server_name localhost;
ssi on;
root /srv/http/city_db/static/;
index index.html;
location / {
# other things you want to set for /
}
}

nginx configuration for rhodecode & redmine on ubuntu 12.04

I am trying to setup rhodecode + redmine on ubuntu with the following
configuration
http://my_ip/redmine
and
http://my_ip/rhodecode
I am using nginx as the web server with redmine running on localhost:3000
and rhodecode running on localhost:5000, somehow iam missing the point in
configuring nginx.conf
I am able to redirect both redmine on port 3000( while testing with webrick) and rhodecode on port 5000 individually but not able to set them as
http://my_ip/redmine
and
http://my_ip/rhodecode
Following is my nginx.conf file
worker_processes 1;
events {
worker_connections 1024;
}
http {
passenger_root /usr/local/rvm/gems/ruby-1.9.3-p374/gems/passenger-3.0.19;
passenger_ruby /usr/local/rvm/wrappers/ruby-1.9.3-p374/ruby;
upstream rhodecode {
server 127.0.0.1:5000;
}
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root /var/data/redmine/public;
passenger_enabled on;
client_max_body_size 25m; # Max attachemnt size
location /rhodecode/ {
try_files $uri #rhodecode;
proxy_pass http://127.0.0.1:5000;
}
location /rhodecode {
proxy_pass http://127.0.0.1:5000;
}
}
}
It will be easier to make subdomains redmine.yousite.com and rhodecode.yoursite.com. It's also prettier and more agile - you can easily move one of the apps to another server.

Resources