nginx Laravel项目仅在main root = project public folder时工作

xmakbtuz  于 2023-04-20  发布在  Nginx
关注(0)|答案(1)|浏览(106)

你好,我试图用nginx服务一个laravel项目,该项目位于:

C:/nginx/html/project_folder

当我在浏览器或服务器名称中转到localhost时,此配置100%工作。主页和所有路由都正常工作。这种方法的问题是url只是localhost,我一次只能处理一个项目。

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        root         C:/nginx/html/project_folder/public;

        location / {
            # root   html;
            index  index.php index.html index.htm;
            try_files $uri $uri/ /index.php$is_args$args;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
           # root           html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
        }
    }
}

当我尝试做下面这样的事情时,我无法再通过localhost/project_name访问laravel项目,localhost/project_name/index.php可以工作,但没有样式化,所有路由都返回“未指定输入文件”,并且只是localhost返回nginx启动页面。

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        root         C:/nginx/html;

        location / {
            # root   html;
            index  index.php index.html index.htm;
            try_files $uri $uri/ /index.php$is_args$args;
        }
        
        location /project_name/public {
            root        C:/nginx/html/project_name/public;
            index       index.php;
            try_files   $uri $uri/ /index.php$query_string;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
           # root           html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
        }
    }
}

我已经在这里搜索了几个小时,不知道为什么会发生这种情况,以及如何解决它。

lymgl2op

lymgl2op1#

我可以通过改变两件事来解决这个问题:
1.将PHP位置块嵌套在/project_name/public位置块中
1.将try文件从/index.php?$query_string;更新为/project_name/public/index.php?$query_string;
最后的工作配置。localhost和localhost/project_name/public沿着所有路由现在都可以工作了。

http {
    include       mime.types;
    default_type  application/octet-stream;

sendfile        on;
keepalive_timeout  65;

server {
    listen       80;
    server_name  localhost;
    root         C:/nginx/html;

    location / {
        index  index.html index.htm;
    }
    
    location /project_name {
        alias       C:/nginx/html/project_name/public;
        index       index.php;
        try_files   $uri $uri/ @project_name;
        
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
           try_files $uri =404;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $request_filename;
           include        fastcgi_params;
        }           
    }

    location @project_name {
        rewrite /project_name/(.*)$ /project_name/index.php?/$1 last;
    }
}

}

相关问题