linux - Nginx laravel 配置

标签 linux laravel nginx

我目前正在 nginx 网络服务器上开发一个 laravel 应用程序,我总是通过编辑/etc/hosts 并为项目名称添加一个条目然后在默认文件中添加一个带有服务器名称的服务器 block 来访问我的项目nginx,所以如果我有一个名为“Missouri”的项目,我会这样调用它:

http://missouri/

我现在想稍微改变一下,在项目名称之前使用我的 IP 地址或本地主机,如下所示:

http://localhost/missouri/

我搜索了很多,找到了很多不同的组合,但没有一个是有效的,这是通用服务器配置的配置 block :

server {
    listen 80 default_server;

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

    server_name localhost;

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

    location ^~  /Missouri {
    alias /var/www/html/Missouri/public;

    try_files $uri $uri/ /Missouri/index.php?$query_string;

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

    }
}

如果我使用以前的配置并调用 localhost/Missouri,我会得到一个文件未找到。白页错误,但是如果我使用以下代码并调用 Missouri/它会起作用:

server {
listen 80;

server_name missouri;

root /var/www/html/Missouri/public;
index index.php index.html;

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

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

如果有任何帮助,我将不胜感激。

最佳答案

我不是 DevOps,但我认为您可以在 nginx 服务器 block 中编写正则表达式,因此“localhost/foo”将成为“localhost/foo/public”的别名。通过这样做,您不需要为每个网站添加 block 。

另一方面,关于您的问题,我建议您使用“root”而不是“alias”。这是一个示例代码:

location /Missouri {
       root /var/www/html/Missouri/public;

       index index.php index.html;

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

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

我希望它有用。

关于linux - Nginx laravel 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42615412/

相关文章:

c - 确定进程是子进程还是父进程(不使用 fork 的返回值)

linux - 从 SpringBoot 应用程序访问 Ubuntu 16.04 上的 MariaDB Galera 集群

javascript - Laravel Ajax 分页链接在第二页点击刷新

javascript - 通过ajax发送的表单的html,jquery自动为其添加结束标记,所以现在表单不起作用

git - 通过 HTTP 和 SSH 通过 Ingress 访问 Kubernetes Git Container

linux - 如何识别crontab child job?

linux - 我的 Raspberry Pi :s clock is drifting, 如何让它尽可能准确?

php - 解析错误 : syntax error, 意外 '?' in/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 233 - laravel

nginx - 带有 Nginx conf 的未知指令 "proxy_cache_revalidate on"

apache - Cloudflare SSL 协议(protocol)错误