算是备忘吧
nginx server 配置:(版本号1.16.1)
1server { 2 listen 80 default_server; 3 listen [::]:80 default_server; 4 server_name xxx.net; 5 # Load configuration files for the default server block. 6 include /etc/nginx/default.d/*.conf; 7 #nginx 本身发生404 500等错误时使用这个 8 fastcgi_intercept_errors on; 9 #nginx转发出去的请求返回404 500等这个才起作用 10 #proxy_intercept_errors 11 #前端配置 访问路径 / 就是访问前端主页面 12 location / { 13 #要想转到自定义的404 500页面 以下这个要配置的 14 root /home/xxx/html/dist; 15 # 不要写成 index index.html,index.html; 访问xxx.net会报403错误 正确写法是 index index.html index.html;不用逗号 用空格 16 #要想转到自定义的404 500页面 以下这个要配置的 17 index index.html; 18 proxy_set_header HOST $host; 19 proxy_set_header X-Real-IP $remote_addr; 20 proxy_set_header X-Forwarded-FOR $proxy_add_x_forwarded_for; 21 } 22 23 # 后端请求地址为http://xxx.net/api开头 此处将/api开头的地址进行转发 24 # 这样做的好处是不用做跨域 因为在同一个域下 25 location ^~ /api { 26 # 下面这一行端口号后面的 / 不要掉 这个 / 代表将上面路径中的 /api去掉 27 # 访问http://xxx.net/api/user/getById?id=1 28 # 会转化成 http://localhost:8080/user/getById?id=1 29 # 如果端口号后没有 / 那么/api会被带上 上面的 30 # 地址就成 http://localhost:8080/api/user/getById?id=1 31 proxy_pass http://localhost:8080/; 32 } 33 34 # 当返回值是404时跳转到/404.html这个url 35 error_page 404 /404.html; 36 #下面这个=不能少 37 location = /404.html { 38 #自定义的404.html放在以下路径 有请求/404.html就使用这个配置 39 root /home/xxx/html/dist; 40 } 41 error_page 500 502 503 504 /50x.html; 42 location = /50x.html { 43 #自定义的50x.html放在以下路径 44 root /home/xxx/html/dist; 45 } 46 }
遇到的问题和注意点都写在注释里面了。