编程

Laravel nginx 配置

618 2023-03-30 18:15:00

如果你的 Laravel 应用部署在运行 Ngnix 的服务器上,你可以使用如下配置作为 web 服务器的配置起点。然后再根据你实际情况修改定制一些配置。

使用如下配置时,请确保你的WEB服务器的所有请求都导向应用 public/index.php 文件。不要尝试将 index.php 文件移到到项目根目录,因为这会导致将许多敏感信息的配置文件暴露到互联网中:

 

server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    root /srv/example.com/public;
 
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
 
    index index.php;
 
    charset utf-8;
 
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
 
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
 
    error_page 404 /index.php;
 
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
    location ~ /\.(?!well-known).* {
        deny all;
    }
}