为客户端路由配置 NGINX

Configure NGINX for client side routing

我在 Digital Ocean 上用 React 运行 制作了一面,我正在尝试使用客户端路由,但无法正常工作。这是我的 Nginx 默认文件

upstream site {
server 123.456.78.90:8080;
 keepalive 64;
}
server {
        server_name site.xyz www.site.xyz;
        try_files $uri /index.html;

        location / {
        index index.html;
        if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        #
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
        }
        if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
        }
        if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
        }

        try_files $uri index.html;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header Host $http_host;
         proxy_set_header X-NginX-Proxy true;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection “upgrade”;
         proxy_max_temp_file_size 0;
         proxy_pass http://site/;
         proxy_redirect off;
         proxy_read_timeout 240s;



        }
        error_page 404 /index.html;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/site.xyz/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/site.xyz/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

server {
    if ($host = www.site.xyz) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = site.xyz) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        server_name site.xyz www.site.xyz;
    return 404; # managed by Certbot

}

我添加了 try_files 行和 error_page 404 /index.html 以便它默认为 index.html 可以从我的代码那里加载路由但是它根本不起作用。我需要 ssl 证书,我需要另一个功能的 cors 代码。我不确定为什么它没有按照我想要的方式引导。

以下是对我有用的方法:

...
location / {
        ...
        proxy_intercept_errors on;
        error_page 404 = /index.html;
        ...
}