【发布时间】:2012-04-29 03:57:11
【问题描述】:
我想将我的服务器的 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 配置文件中的服务器指令:
rewrite ^ $scheme://mydomain.com$request_uri redirect;
但我的浏览器中出现太多重定向错误。
至少,我可以通过在服务器指令中使用它来防止显示 IP 地址:
if ($host !~* ^www\.) {
rewrite ^(.*)$ http://www.$host$1 permanent;
}
然而,这被列为 nginx 网站上的陷阱之一:(
非常感谢任何关于我可能做错的见解!
谢谢!
【问题讨论】:
标签: nginx ip forwarding unicorn