1

nginx 多个网站在同一个端口和一个 ip 地址

例如我的 ip 192.167.10.2

我们想在相同的 ip nginx/html 上运行站点包含两个项目,如下所示

1:项目1 2:项目2

我们在 ip :192.167.10.2 上运行默认 project1,第二个项目像 192.167.10.2/project2 一样运行

如何在 nginx 配置文件中进行配置

4

2 回答 2

0

这是一个简单的例子。你可以试试

第一个配置:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/mywebsite1;
    index index.php index.html index.htm;

    server_name mywebsite1.de www.mywebsite1.de;

    location / {
    try_files $uri/ /index.php?$args;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /var/www/mywebsite1;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

第二个配置:

server {
    listen 80;
    listen [::]:80 //@here Be carefull , only one default

    root /var/www/mywebsite2;
    index index.php index.html index.htm;

    server_name mywebsite1.de www.mywebsite1.de;

    location / {
        try_files $uri/ /index.php?$args;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /var/www/mywebsite1;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

不要忘记重新启动服务器。

于 2018-10-23T10:39:03.010 回答
0

我认为你应该试试这个。但是您可能需要一些针对不同情况的附加选项。

静态站点的配置:

http {
    server {
        location / {
            root /var/www/site1;
        }
    
        location /project2/ {
            root /var/www/site2;
        }
    }
}

对于应用程序30003001端口。

http {
    server {
        location /project2 {
            proxy_pass http://127.0.0.1:3001$request_uri;
        }

        location / {
            proxy_pass http://127.0.0.1:3000$request_uri;
        }
    }
}
于 2021-02-02T09:46:07.677 回答