Rewrite - forward server IP to domain url nginx
Solution 1
Actually the answer above does not work (at least for me). I suppose since the question has been raised the asker has found his solution, but since I searched on google and a lot of links go here, maybe this solution will help other people, simply add this to your conf file:
server {
server_name 162.13.171.178;
# you can add other server_name if you need other IPs
# or domain names such as without the www
add_header X-Frame-Options "SAMEORIGIN";
return 301 $scheme://www.example.com$request_uri;
}
Solution 2
It's also possible to use one server block to redirect from many IPs.
server {
listen 80;
listen 443;
server_name 172.20.0.2 172.20.0.3 172.20.0.4;
return 301 $scheme://www.yourdomain.com$request_uri;
}
There happens to also be a routine PCI compliance finding (leaking private IPs) that can be resolved using this strategy.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on June 17, 2022Comments
-
Admin about 2 years
I'd like to have the IP of my server be rewritten as a domain name url but I'm having the hardest time figuring out how to make this happen.
For example, when I enter 213.34.54.xxx in the browser, I'd like it to be rewritten as mydomain.com and NOT display the IP address.
My current configuration is as follows:
/etc/nginx/nginx.conf
user www-data; worker_processes 2; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; server_names_hash_bucket_size 64; sendfile on; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/mydomain; }
/etc/nginx/sites-enabled/mydomain
upstream unicorn { server unix:/tmp/unicorn.mydomain.sock fail_timeout=0; } server { listen 80 default; server_name localhost; root /home/deployer/apps/mydomain/current/public; location ^~ /assets/ { gzip_static on; expires max; add_header Cache-Control public; } try_files $uri/index.html $uri @unicorn; location / { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://unicorn; } error_page 500 502 503 504 /500.html; client_max_body_size 4G; keepalive_timeout 10; }
I've tried adding this to the server directive in mydomain configuration file:
rewrite ^ $scheme://mydomain.com$request_uri redirect;
but I get a TOO MANY REDIRECTS ERROR in my browsers.
At the very least, I'm able to prevent the IP address from being displayed by using this in the server directive:
if ($host !~* ^www\.) { rewrite ^(.*)$ http://www.$host$1 permanent; }
However, this is listed as one of the pitfalls on the nginx site :(
Any insights of what I might be doing wrong would be GREATLY appreciated!
Thanks!
-
Michael Jess over 7 yearsIt's possible to list multiple
server_name
entries within one server block. Your last two blocks could be condensed into one. -
Is Ma over 7 yearsCould anyone help me to edit my solution following the advice of crftr? =)