如何在Nginx上用相同的URL配置多个站点

时间:2019-01-20 16:27:11

标签: nginx slim nginx-location nginx-config

我正在尝试使两个网站以相同的地址运行,

我希望my.website.com/test_slim上的所有请求以及每个my.website.com/test_slim/anything/following上的所有请求都指向/var/www/html/test_slim/src/public/index.php

我希望将其他所有内容(my.website.com/my.website.com/foomy.website.com/bar/baz ...)用作/var/www/html/whatever/file/according/to/url/index.php中的普通PHP文件

现在,这是我的服务器配置:

/etc/nginx/site-enabled/my-default

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

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

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}

/etc/nginx/sites-enabled/test_slim

server {
    listen 80;
    server_name _;
    index index.php;
    error_log /var/www/html/test_slim/error.log;
    access_log /var/www/html/test_slim/access.log;
    root /var/www/html/test_slim/src/public;

    location /test_slim/ {
        try_files $uri /index.php$is_args$args;
    }

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

但是我实际上得到的是:

  • localhost/的请求根据需要执行/var/www/html/index.html
  • localhost/toto的请求根据需要执行/var/www/html/toto/index.html
  • localhost/test_slim的请求下载 /var/www/html/test_slim/src/public/index.pxp
  • localhost/test_slim/hello的请求返回一个Nginx错误页面(如果hello文件夹不存在,则返回404,如果不存在,则返回403)。
  • localhost/test_slim/src/public/的请求会执行/var/www/html/test_slim/src/public/index.php文件。

0 个答案:

没有答案