简体   繁体   English

NGINX server_name 不工作 2

[英]NGINX server_name not work 2

My website always open in path localhost, but my server_name have other domen name.我的网站总是在路径 localhost 中打开,但我的 server_name 有其他域名称。 How i can fix it ?我该如何解决? My configuration我的配置

https://i.stack.imgur.com/MXm5k.jpg https://i.stack.imgur.com/MXm5k.jpg

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {

    server {
        listen 80;
        server_name  mydomain;

        #charset koi8-r;

        access_log  logs/host.access.log;

        location / {
            proxy_pass http://127.0.0.1:3037;
        }

    }

}

Change your config to below将您的配置更改为以下

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {

    server {
        listen 80 default_server;
        return 403;
    }

    server {
        listen 80;
        server_name  mydomain;

        #charset koi8-r;

        access_log  logs/host.access.log;

        location / {
            proxy_pass http://127.0.0.1:3037;
        }

    }

}

First server block is the default server nginx will serve the request from if no virtual host matches.如果没有虚拟主机匹配,第一个服务器块是默认服务器 nginx 将提供请求。 So you need to have 2 blocks in case you only want specific server_name to be allowed and rest all to be denied所以你需要有 2 个块,以防你只希望允许特定的server_name而其余的都被拒绝

For testing and accepting doing a "catch-all", you can use server_name _为了测试和接受做一个“包罗万象”,你可以使用server_name _

From: http://nginx.org/en/docs/http/server_names.html来自: http : //nginx.org/en/docs/http/server_names.html

In catch-all server examples the strange name “_” can be seen:在包罗万象的服务器示例中,可以看到奇怪的名称“_”:

server {
    listen       80  default_server;
    server_name  _;
    return       444; 
}

You have to match your custom domain name to your machine's local IP address.您必须将自定义域名与机器的本地 IP 地址匹配。 This can be done using the default 127.0.0.1 or by typing the command, "ip addr" in your Ubuntu terminal.这可以使用默认的 127.0.0.1 或通过在 Ubuntu 终端中键入命令“ip addr”来完成。 this command will list out two IP addresses offered by you machine.此命令将列出您的机器提供的两个 IP 地址。 You can match any of the IP addresses to your custom domain in the "/etc/hosts" file.您可以将任何 IP 地址与“/etc/hosts”文件中的自定义域进行匹配。

如果您使用的是 Ubuntu,您还必须在/etc/hosts为您的本地 ip 定义您的服务器名称: 127.0.0.1 mydomain www.mydomain.com mydomain.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM