How to host 2 websites at same vps using nginx

问题描述 投票:0回答:2

我有 2 个域,我们称它们为 mydomain1.com 和 mydomain2.com

我也有一个 VPS,我正在尝试将它们都托管在同一个 VPS 上,但是没有 mydomain1.com:port

此外,如果有帮助,我会使用翼龙面板来托管它们 代码:(两者相同,只是第二个有不同的域)

server {
    listen 20002;
    server_name mydomain1.com;

    #access_log /home/container/naccess.log;
    #error_log  /home/container/nerror.log error;

    root /home/container/webroot;
    index index.html index.htm index.php;
    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    # allow larger file uploads and longer script runtimes
    client_max_body_size 100m;
    client_body_timeout 120s;
    sendfile off;
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/home/container/tmp/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param PHP_VALUE "upload_max_filesize = 100M \n post_max_size=100M";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTP_PROXY "";
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    }

    location ~ /\.ht {
        deny all;
    }
}

我尝试将第一个 server_name 更改为 mydomain1.com,将第二个 server_name 更改为 mydomain2.com,但我只能使用 mydomain1.com:port 或 mydomain2.com:port 访问站点,因为它们未托管在端口 80 上。

nginx vps
2个回答
1
投票

如果您想为每个域名使用不同的后端,这就是 NGINX 配置的样子。根据需要调整。

此配置侦听端口 80 和 443 上的流量。顺便说一句,上游端口可以相同或不同。

如果从 80 端口访问域 one.com,它将提供 1.1.1.1:2000 的内容

如果从 443 端口访问域 two.com,它将提供 2.2.2.2:2001 的内容

http {
    upstream one {
        server 1.1.1.1:2000;
    }

    upstream two {
        server 2.2.2.2:2001;
    }

    server {
        listen 80;
        server_name one.com;

        location / {
            proxy_pass http://one;
        }
    }

    server {
        listen 443 ssl;
        server_name two.com;

        ssl_certificate /path/to/certificate.pem;
        ssl_certificate_key /path/to/private.key;

        location / {
            proxy_pass https://two;
        }
    }
}

如果您想监听单个端口,如端口 4000,并将流量适当地定向到后端,那么这将是一个示例配置:

http {
    upstream one {
        server 1.1.1.1:2000;
    }

    upstream two {
        server 2.2.2.2:2001;
    }

    server {
        listen 4000;
        server_name one.com two.com;

        location / {
            if ($http_host = "one.com") {
                proxy_pass http://one;
            }
            if ($http_host = "two.com") {
                proxy_pass http://two;
            }
        }
    }
}

0
投票

如果您的 VPS 上还没有安装 Nginx,请从 Linux 发行版的包管理器中安装它。

Change the nginx.conf file: Go to the Nginx root folder and locate the nginx.conf file. Ensure the conf file contains:
   include /etc/nginx/conf.d/*.conf;

查看启动中特定位置的文件配置。您可以在那里创建配置文件来托管网站。

您可以创建服务器块来托管多个域,例如 abc.com 和 abc1.com 在 NGINX 根文件夹 /etc/nginx/conf.d/ 中创建 abc.com.conf 以保存 abc.com 的配置

server {
    listen 80 default_server;
    listen [::]:80 default_server;  
    root /var/www/abc.com;  
    index index.html;  
    server_name abc.com www.abc.com;    
    location / { try_files $uri $uri/ =404;}
}

在 NGINX 根文件夹 /etc/nginx/conf.d/ 中创建 abc1.com.conf。保存 abc1.com 的配置

server {
    listen 80;
    listen [::]:80;  
    root /var/www/abc1.com;  
    index index.html;  
    server_name abc1.com abc1.com;      
    location / { try_files $uri $uri/ =404;}
}

创建文件夹来托管网站文件:

在/var/www/下创建两个目录abc.com和abc1.com。 上传网站文件到主机 现在放置您的静态文件内容,以便服务器可以找到它们。下一步是导航到 /var/www/abc.com 和 /var/www/abc1.com 并将站点的 HTML 和静态文件放在那里。

要应用新配置,请使用重新启动网络服务器 sudo systemctl 重启 nginx

© www.soinside.com 2019 - 2024. All rights reserved.