Laravel 部署:Nginx 配置
Nginx 虚拟主机配置
如果你把应用程序部署到执行 Nginx
的服务器,你可以使用下面的配置内容来开始设置你的 Web
服务器。此配置内容最好根据你的服务器需求做一些自定义的修改。如果你需要托管你的服务器,可以考虑 Laravel Forget 等服务:
server {
listen 80;
server_name example.com;
root /example.com/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm 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_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
开启 Gzip 压缩
开启 Gzip 能让 Nginx 压缩一些文件,从而加载速度更快。
sudo vim /etc/nginx/nginx.conf
把 # 注释去掉。示例:
gzip on;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript
软重启 Nginx
sudo systemctl reload nginx
测试是否生效:
curl -H "Accept-Encoding: gzip" -I http://www.example.com
返回 Content-Encoding: gzip
即为成功。示例:
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 26 Jun 2019 03:47:49 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Content-Encoding: gzip