Nginx 前端服务器 转发后端请求 自定义错误页面

算是备忘吧

nginx server 配置:(版本号1.16.1)

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  xxx.net;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        #nginx 本身发生404 500等错误时使用这个
        fastcgi_intercept_errors on;
        #nginx转发出去的请求返回404 500等这个才起作用
        #proxy_intercept_errors
		#前端配置 访问路径 / 就是访问前端主页面
        location / {
        	#要想转到自定义的404 500页面  以下这个要配置的
            root /home/xxx/html/dist;
            # 不要写成  index index.html,index.html; 访问xxx.net会报403错误 正确写法是  index index.html index.html;不用逗号  用空格
            #要想转到自定义的404 500页面  以下这个要配置的
            index index.html;
            proxy_set_header HOST   $host;
            proxy_set_header X-Real-IP      $remote_addr;
            proxy_set_header X-Forwarded-FOR $proxy_add_x_forwarded_for;
        }

		# 后端请求地址为http://xxx.net/api开头 此处将/api开头的地址进行转发
		# 这样做的好处是不用做跨域 因为在同一个域下
        location ^~ /api {
            # 下面这一行端口号后面的 / 不要掉 这个 / 代表将上面路径中的 /api去掉
            # 访问http://xxx.net/api/user/getById?id=1 
            # 会转化成 http://localhost:8080/user/getById?id=1
            # 如果端口号后没有 / 那么/api会被带上 上面的
            # 地址就成 http://localhost:8080/api/user/getById?id=1
            proxy_pass http://localhost:8080/;
        }

		# 当返回值是404时跳转到/404.html这个url
        error_page 404 /404.html;
        #下面这个=不能少
        location = /404.html {
        	#自定义的404.html放在以下路径 有请求/404.html就使用这个配置
        	root /home/xxx/html/dist;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            #自定义的50x.html放在以下路径
        	root /home/xxx/html/dist;
        }
    }

遇到的问题和注意点都写在注释里面了。

猜你喜欢

转载自blog.csdn.net/u013014691/article/details/115804137
今日推荐