nginx:如何在 nginx 中添加新站点/服务器名称?

Neo*_*Neo 2 nginx

我刚刚开始在我的 Ubuntu 10.04 上探索 Nginx。我安装了 Nginx,并且能够在本地主机上获得“欢迎使用 Nginx”页面。但是,即使我在站点可用/默认文件中进行了更改,我也无法添加新的 server_name。尝试重新加载/重新启动 Nginx,但没有任何效果。一个有趣的观察。浏览器中的“ http://mycomputername ”有效。所以不知何故有一个命令像'server_name $hostname' 某处覆盖了我的规则。

文件:sites-available/mine.enpass

server {
   listen   80;
   server_name  mine.enpass ;

   access_log  /var/log/nginx/localhost.access.log;

   location / {
    root   /var/www/nginx-default;
    index  index.html index.htm;
   }
}
Run Code Online (Sandbox Code Playgroud)

文件:nginx.conf

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
worker_connections  1024;
# multi_accept on;
}

http {
include       /etc/nginx/mime.types;

access_log  /var/log/nginx/access.log;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;
tcp_nodelay        on;

gzip  on;
gzip_comp_level 2;
gzip_proxied any;
gzip_types      text/plain text/css application/x-javascript text/xml application/xml    application/xml+rss text/javascript;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Run Code Online (Sandbox Code Playgroud)

dav*_*ams 5

我的建议是通过删除 /etc/nginx/sites-enabled 中的符号链接来删除默认站点:

$ sudo rm /etc/nginx/sites-enabled/default
Run Code Online (Sandbox Code Playgroud)

然后在新文件中创建所需的配置,将其命名为 /etc/nginx/sites-available/your.host.name。假设您的主页位于 /var/www/your.host.name,这是一个非常简单的示例:

server {
        listen 80;
        server_name your.host.name;

        location / {
                root   /var/www/your.host.name;
                index  index.html;
        }
}
Run Code Online (Sandbox Code Playgroud)

然后在 /etc/nginx/sites-enabled 中创建一个符号链接:

$ sudo ln -s /etc/nginx/sites-available/your.host.name /etc/nginx/sites-enabled/your.host.name
Run Code Online (Sandbox Code Playgroud)

最后,重启nginx:

$ sudo /etc/init.d/nginx restart
Run Code Online (Sandbox Code Playgroud)

祝你好运。