首页 > 解决方案 > 多个站点的 nginx 配置一个 ip 地址和一个服务器

问题描述

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 配置文件中进行配置

标签: nginxmultisitenginx-config

解决方案


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

第一个配置:

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;
    }
}

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


推荐阅读