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

Cannot access domains hosted on Nginx

· 52CE 我爱测

I have taken the task to try and figure out how to use Nginx to host my websites (previously using IIS). I purchased a VPS running CentOS 8. I have been trying to configure Nginx to run two different domains on the same port. I h…

问题原文

原帖链接 · beaver559 · CC BY-SA 4.0

I have taken the task to try and figure out how to use Nginx to host my websites (previously using IIS). I purchased a VPS running CentOS 8. I have been trying to configure Nginx to run two different domains on the same port.

I have examined and followed multiple guides online and I am still unable to target both websites.

After installing Nginx and configuring the service, I created two conf files in /etc/nginx/conf.d directory:

website1.com.conf

server {
    listen 80;
    root /var/www/website1.com;
    index index.html index.htm;
    server_name website1.com www.website1.com;

    location / {
            try_files $uri $uri/ =404;
    }

}

website2.com.conf

server {
    listen 80;
    root /var/www/website2.com;
    index index.html index.htm;
    server_name website2.com www.website2.com;

    location / {
            try_files $uri $uri/ =404;
    }

}

I then also created two basic index.html files in the directory /var/www/website(1|2).com

Initially I had to remove the default server block from the nginx.conf file to get it to load anything other that the default site.

nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

}

I have been trying to load the website using this URL with my IP address and also localhost (http://0.0.0.0/website1). It is my understanding Nginx matches incoming requests first against port then by server name, both of my config files are on listening on port 80.

I would really appreciate some direction on what I am doing wrong here as everything I have seems to follow documentation.

已采纳解答

原帖链接 · Mircea Vutcovici · CC BY-SA 4.0

You need to use the hostname (e.g. website2.com) in the URL. That hostname should be in DNS or, for testing, in /etc/hosts or equivalent for your workstation OS.

Edit: Here is how the web browser is working. When you have an URI (URL), the string is split in multiple components (protocol, user, password, host, path, query, fragment...)

name resolution - Host is the one we are interested. It can be a hostname or an IP. If it is a hostname, the name will be resolved by the resolver library located on the web client machine. This library by default will use hosts file, then DNS to resolve the name.

virtual host aka nginx Server Blocks - the web browser will make a HTTP request that will contain Host: header to tell the web server which hostname he is trying to access. That is why you need to have a hostname and not an IP if you want to test those servers.

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

资料来源与适用范围

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

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

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