大网数据广告活动
服务器运维问答

setup nginx with IP as server name

· 52CE 我爱测

How to setup nginx with IP address as server name? server { listen 80; server_name xx.xx.xx.xx; location /test { root /var/www/test; } location ~ \.php$ { include /var/ini/nginx/fastcgi.conf; fastcgi_pass php; fastcgi_index index…

问题原文

原帖链接 · clarkk · CC BY-SA 3.0

How to setup nginx with IP address as server name?

server {
    listen  80;
    server_name  xx.xx.xx.xx;

    location /test {
        root  /var/www/test;
    }

    location ~ \.php$ {
        include  /var/ini/nginx/fastcgi.conf;
        fastcgi_pass  php;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/test$fastcgi_script_name;
    }
}

So I want to access the server like this

http://xx.xx.xx.xx/test/ => index.php
http://xx.xx.xx.xx/test/foo.php => foo.php

社区高分解答

原帖链接 · Joshua Griffiths · CC BY-SA 3.0

You can just put the IP address as the server name:

server {
    listen 80;
    server_name 192.168.1.21;
    ...
}

You may also want to change the configuration to only listen on the specified IP address:

server {
    listen 192.168.1.21:80;
    server_name 192.168.1.21;
    ...
}

The following is from the docs: http://nginx.org/en/docs/http/server_names.html

If someone makes a request using an IP address instead of a server name, the “Host” request header field will contain the IP address and the request can be handled using the IP address as the server name:

server {
    listen       80;
    server_name  example.org
                 www.example.org
                 ""
                 192.168.1.1
                 ;
    ...
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-07-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。