Nginx 在/var/www中有多个项目的服务器?

Nginx 在/var/www中有多个项目的服务器?,nginx,virtualhost,Nginx,Virtualhost,我有一个服务器,我想用作个人服务器,我的所有项目都在/var/www下 我目前有两个文件夹,/var/www/html和/var/www/site 我希望能够通过以下URL访问这些文件夹(123.123.123.123是我的服务器IP): 123.123.123.123/html和123.123.123/site 这是我的默认虚拟主机文件: server { listen 80 default_server; listen [::]:80 default_server;

我有一个服务器,我想用作个人服务器,我的所有项目都在
/var/www

我目前有两个文件夹,
/var/www/html
/var/www/site

我希望能够通过以下URL访问这些文件夹(
123.123.123.123
是我的服务器IP):

123.123.123.123/html
123.123.123/site

这是我的
默认
虚拟主机文件:

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

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}
这是我为
/var/www/site
创建的一个,名为
site

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

    # Because this site uses Laravel, it needs to point to /public
    root /var/www/site/public;

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

    server_name 123.123.123.123;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}
但是当我转到
123.123.123.123/site
,它说404找不到,所以很明显我做错了什么(是的,我重新启动了nginx)


请帮忙

您只需要一个
服务器
块,因为
/html
/site
都位于同一个
服务器

使用
nginx-t
检查
nginx
是否真的重新启动而没有出现任何错误

由于
/site
使用复杂的目录方案,因此需要使用嵌套的
位置
块来获得正确的路径

您的第一个项目似乎有一个简单的静态和PHP文件安排。您可以使用
root/var/www语句将以
/html
开头的URI映射到
html
文件夹。更多信息,请参阅

像这样的东西可能适合你:

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

    root /var/www/;

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

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }

    location ^~ /site {
        alias /var/www/site/public;
        if (!-e $request_filename) { rewrite ^ /site/index.php last; }

        location ~ \.php$ {
            if (!-f $request_filename) { return 404; }

            include snippets/fastcgi-php.conf;
            fastcgi_param  SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    }
}

默认服务器不需要
服务器名称

我有一个问题。如果我转到某个路径,比如
123.123.123.123/site/search
,它返回404,但是索引(
123.123.123.123/site
)工作正常。我应该怎么做才能修复此问题,使路由正常工作?如果有帮助,这里是
.htaccess
文件:我在
.htaccess
文件中看不到任何奇怪的东西。基本检查:清除浏览器缓存,使用
nginx-t
和/或
nginx-t
测试配置,并查看访问和错误日志以了解发生的情况。