我目前正在 nginx 网络服务器上开发一个 laravel 应用程序,我总是通过编辑/etc/hosts 并为项目名称添加一个条目然后在默认文件中添加一个带有服务器名称的服务器 block 来访问我的项目nginx,所以如果我有一个名为“Missouri”的项目,我会这样调用它:
我现在想稍微改变一下,在项目名称之前使用我的 IP 地址或本地主机,如下所示:
我搜索了很多,找到了很多不同的组合,但没有一个是有效的,这是通用服务器配置的配置 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/