部署 Laravel 和 Vue 前后端分离项目

Nginx 配置

主要利用 Nginx 的反向代理功能,前后端项目通过代理到不同的端口号,来区分 Laravel 和 Vue 单页面项目。

Nginx 配置文件

server {
  listen 8282;# 后端端口号
  root /www/laravel/public;
  index index.php;
  server_name localhost;
  charset utf-8;

  add_header X-Frame-Options "SAMEORIGIN";
  add_header X-Content-Type-Options "nosniff";

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

  # access_log  /var/log/nginx/nginx.laravel.access.log  main;
  error_log  /var/log/nginx/nginx.laravel.error.log warn;

  error_page 404 /index.php;

  location ~ \.php$ {
    fastcgi_pass        127.0.0.1:9000;
    fastcgi_buffers     16 16k;
    fastcgi_buffer_size 32k;
    include             fastcgi-php.conf;
    include             fastcgi_params;
  }
}

An5dyNginxNginx大约 1 分钟
隐藏 Nginx 和 PHP 版本号

当访问以 Nginx 为 web 服务器,PHP 为脚本的网站时,默认会在 header 头中显示 NginxPHP 的版本号,这些暴露出来的信息在一定程度上存在安全隐患(当黑客攻击服务器时,会收集服务器相关信息,比如软件的版本等,然后利用这些信息,挖掘相关漏洞来对服务进行攻击),所以在一定程度上隐藏这些信息是十分有必要的。

通过请求可以获取如下信息:

curl -i http://test.it

HTTP/1.1 200 OK
Server: nginx/1.15.8
...
X-Powered-By: PHP/7.2.19

An5dyNginxNginx小于 1 分钟