サーバーの IP をドメイン名の URL として書き換えたいのですが、これを実現する方法を理解するのに最も苦労しています。
たとえば、ブラウザに 213.34.54.xxx と入力すると、mydomain.com に書き換えられ、IP アドレスが表示されないようにしたいと考えています。
私の現在の構成は次のとおりです。
/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;
}
これをmydomain構成ファイルの server ディレクティブに追加しようとしました:
rewrite ^ $scheme://mydomain.com$request_uri redirect;
しかし、ブラウザで TOO MANY REDIRECTS ERROR が表示されます。
少なくとも、サーバー ディレクティブでこれを使用することで、IP アドレスが表示されないようにすることができます。
if ($host !~* ^www\.) {
rewrite ^(.*)$ http://www.$host$1 permanent;
}
ただし、これはnginxサイトの落とし穴の1つとしてリストされています:(
私が間違っているかもしれないことについての洞察は大歓迎です!
ありがとう!