Nginx laravel配置

问题描述:

我目前正在nginx网络服务器上开发一个laravel应用程序,我总是通过编辑/ etc/hosts来访问我的项目,并添加一个项目名称,然后添加一个服务器块在nginx的默认文件,所以如果我有一个所谓的“密苏里”号的项目,我想这样称呼它:Nginx laravel配置

http://missouri/

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

http://localhost/missouri/

我寻觅了很多

,发现了很多不同的组合,但没有一个高效的,这是一般的服务器配置的配置块:

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我找不到文件。白色页面错误,但是如果我用下面的代码,并呼吁密苏里州/它的工作原理:

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服务器块中编写正则表达式,因此“localhost/foo”将成为“localhost/foo/public”的别名。通过这样做,你不需要为每个网站添加块。

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

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

我希望它的作品。

+0

感谢您的评论,不幸的是它并没有为我工作。 –