通过 IP 打开网址


Open URL via IP

我有带有nginx服务器的远程服务器机器。我创建了sites-availablesites-enabled文件夹,其中有一个名为example.com的文件夹,具有以下配置:

#HTTP serve
#
server {
        listen   80;
        root /var/www/example.com/www/;
        index index.php index.html index.htm;
        server_name example.com;
        access_log /var/log/nginx/example.com.access.log combined buffer=1024k;
        error_log /var/log/nginx/example.com.error.log;
        client_max_body_size 128M;
        if (!-e $request_filename) {
                rewrite ^/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ /index.php?controller=$1&action=$2 last;
        }
        location ~ '.php$ {
                if (!-f $document_root/$fastcgi_script_name){
                        return 404;
                }
                fastcgi_split_path_info ^(.+'.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                proxy_connect_timeout 600s;
                proxy_read_timeout 600s;
                include fastcgi_params;
        }
        location ~ /'.ht {
                deny all;
        }
}

当我在浏览器中输入 url 时http://example.com/它应该将我重定向到/var/www/example.com/www。但是我还没有这个域,我想通过服务器IP xxx.xxx.xxx.xxx打开它。我该怎么做?

我应该将文件夹重命名为 IP 吗?那时我无法访问其他文件夹。

如@ceejayoz所述并由 http://nginx.org/en/docs/http/server_names.html 确认,您可以在server_name下指定 IP 地址:

如果有人使用 IP 地址而不是服务器名称发出请求,

则"主机"请求标头字段将包含 IP 地址,并且可以使用 IP 地址作为服务器名称来处理请求:

否则,默认操作是在不匹配server_name的情况下使用配置文件中的第一个服务器。

http://nginx.org/en/docs/http/request_processing.html#mixed_name_ip_based_servers

在此配置中,nginx仅测试请求的标头字段"Host",以确定应将请求路由到哪个服务器。如果其值与任何服务器名称都不匹配,或者请求根本不包含此标头字段,则nginx会将请求路由到此端口的默认服务器。在上面的配置中,默认服务器是第一个 - 这是nginx的标准默认行为。

您可以查看server_name的default_server选项,如第二个链接中所述。