一、针对 browser history 模式下服务器配置
browser history 是React Router 官方推荐的 history。它使用浏览器中的 History API 处理 URL ,会创建一个像 example.com/some/path 这样真实的 URL 。
服务器配置
服务器需要做好处理 URL 的准备。处理最初的 / 这样的请求应该没问题,但当用户来回跳转并在 /accounts/123 页面刷新时,服务器就会收到来自 /accounts/123 的请求,由于服务端无法查找到该资源,便会报 404 错误。
针对上述情况,一个 express 的服务端应用配置可能像这样的:
const express = require('express')const path = require('path')const port = process.env.PORT || 8080const app = express()// 通常用于加载静态资源app.use(express.static(__dirname + '/public'))// 在你应用 JavaScript 文件中包含了一个 script 标签// 的 index.html 中处理任何一个 routeapp.get('*', function (request, response){response.sendFile(path.resolve(__dirname, 'public', 'index.html'))})app.listen(port)console.log("server started on port " + port)
如果你的服务器是 nginx,则使用 try_files 指令:
server {...location / {try_files $uri /index.html}}
当在服务器上找不到其他文件时,这可以让 nginx 服务器提供静态文件服务并指向index.html 文件。
注:
上述情况通常发生在browserHistory模式下,hashHistory模式下则不存在上述问题。
二、Nginx 配置样例
以下是将前端 SPA 应用私有化部署到他人服务器上的 Nginx 配置样例。
# 指定用户:他人分配的账号user shimu;worker_processes auto;# 指定log输出目录error_log /home/shimu/nginx/error.log;pid /run/nginx.pid;# Load dynamic modules.include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;}http {log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;include /etc/nginx/mime.types;default_type application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf;server {listen 80 default_server;listen [::]:80 default_server;server_name _;# 指定文件路径root /home/shimu/fengrong/web/dist;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;# 若当前文件不存在,则进行重定向。location / {if (!-e $request_filename) {rewrite ^/(.*) /index.html last;break;}}location /api {proxy_pass http://127.0.0.1:8800/api;proxy_set_header Host $host;}error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}}
其中:
- user:nginx 运行后可以指定用户,比如说一个静态网页服务器的文件目录的不同的用户有不同的访问权限,使用 nginx 指定用户就可以有权限对此目录读写,详见链接;
- root:root 是 web 根目录,建议使用绝对路径。相对路径受制于nginx的工作目录,存在不确定性;
- $request_filename:当前请求的文件路径,由 root 或 alias 指令与URI请求生成;
- -e和!-e:用来判断是否存在文件或目录;
- rewrite:rewrite是 nginx 中的重定向指令。
^/(.*)是重定向规则,/index.html重定向路径。
三、解决 Nginx 的 “403 Forbidden” 报错
引起 “403 Forbidden” 有两种原因,一是缺少索引文件,二是权限问题。
1) 缺少 index.html
server {listen 80;server_name localhost;index index.html;root /home/shimu/web;
如果在/home/shimu/web下面没有index.html,此时访问域名(www.example.com) 会找不到文件, Nginx 便会报“403 Forbidden”错误。
针对这一问题,首先查看下 root 是否使用的是绝对路径。不要使用相对路径,因为相对路径受制于Nginx的工作目录,存在不确定性。
2) 权限问题
因为权限问题引起的 403。通常我们会将 web 目录放在用户的所属目录下面,nginx 的启动用户默认是 nginx 的,所以对目录根本没有读的权限,这样就会报403错误了。这个时候,把web目录的权限改大,或者是把nginx的启动用户改成目录的所属用户,重起一下就能解决。
# nginx的默认启动用户是nginx# user nginx# web 目录下所在的用户user shimu;
