配置 nginx web 服务器以允许使用主机名而不是 IP 进行本地访问

配置 nginx web 服务器以允许使用主机名而不是 IP 进行本地访问

我可以访问http://10.120.11.31/但无法访问http://mybox/。设备是 raspberry pi,网络服务器是 nginx。我宁愿不在设备上安装 dns 服务器,并认为放入10.120.11.31 mybox/etc/host编辑,我最初意外地显示/etc/hostname为 EEAA 指示)将使它不再需要。这是如何实现的?

michael@mybox:~ $ cat /etc/hostname
mybox
michael@mybox:~ $ cat /etc/hosts
127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

127.0.1.1       mybox
10.120.11.31    mybox
michael@mybox:~ $ cat /etc/dhcpcd.conf
# A sample configuration for dhcpcd.
# See dhcpcd.conf(5) for details.

# Allow users of this group to interact with dhcpcd via the control socket.
#controlgroup wheel

# Inform the DHCP server of our hostname for DDNS.
hostname

# Use the hardware address of the interface for the Client ID.
clientid
# or
# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361.
#duid

# Persist interface configuration when dhcpcd exits.
persistent

# Rapid commit support.
# Safe to enable by default because it requires the equivalent option set
# on the server to actually work.
option rapid_commit

# A list of options to request from the DHCP server.
option domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
# Most distributions have NTP support.
option ntp_servers
# Respect the network MTU.
# Some interface drivers reset when changing the MTU so disabled by default.
#option interface_mtu

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate Stable Private IPv6 Addresses instead of hardware based ones
slaac private

# A hook script is provided to lookup the hostname if not set by the DHCP
# server, but it should not be run by default.
nohook lookup-hostname

interface eth0
static ip_address=10.120.11.31/24
static routers=10.120.11.1
static domain_name_servers=10.120.11.1
michael@mybox:~ $ cat /etc/nginx/sites-available/default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
        listen 80;
        server_name $domain_name mybox;
        root /var/www/html/;
        index index.html index.htm index.php;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        location ~\.php{
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param HTTPS off;
                try_files $uri =404;
                include fastcgi_params;
        }
        try_files $uri $uri/ /index.php;
}
michael@mybox:~ $

答案1

并认为放入10.120.11.31 mybox后就/etc/hostname不再需要了。

/etc/hostname与您的客户端解析名称的能力无关。您考虑的是/etc/hosts,在您的客户。如果您这样做,名称应该能够得到正确解析。

相关内容