Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nginx如何在使用React路由器代理POST请求时为index.html提供服务;组织定位_Html_Http_Nginx_Reactjs_React Router - Fatal编程技术网

Nginx如何在使用React路由器代理POST请求时为index.html提供服务;组织定位

Nginx如何在使用React路由器代理POST请求时为index.html提供服务;组织定位,html,http,nginx,reactjs,react-router,Html,Http,Nginx,Reactjs,React Router,我不知道如何让nginx使用React路由器的HistoryLocation配置为静态文件提供服务。我尝试过的设置要么阻止我刷新或访问url作为顶部位置(使用404无法获取/…),要么阻止我提交POST请求 以下是我的初始nginx设置(不包括我的mime.types文件): nginx.conf # The maximum number of connections for Nginx is calculated by: # max_clients = worker_processes * w

我不知道如何让nginx使用React路由器的HistoryLocation配置为静态文件提供服务。我尝试过的设置要么阻止我刷新或访问url作为顶部位置(使用404无法获取/…),要么阻止我提交POST请求

以下是我的初始nginx设置(不包括我的mime.types文件):

nginx.conf

# The maximum number of connections for Nginx is calculated by:
# max_clients = worker_processes * worker_connections
worker_processes auto;

# Process needs to run in foreground within container    
daemon off;

events {
  worker_connections 1024;
}

http {
  # Hide nginx version information.
  server_tokens off;

  # Define the MIME types for files.
  include       /etc/nginx/mime.types;

  # Update charset_types due to updated mime.types
  charset_types
    text/xml
    text/plain 
    text/vnd.wap.wml
    application/x-javascript
    application/rss+xml
    text/css
    application/javascript
    application/json;

  # Speed up file transfers by using sendfile() to copy directly
  # between descriptors rather than using read()/write().
  sendfile      on;

  # Define upstream servers
  upstream node-app {
    ip_hash;
    server 192.168.59.103:8000;
  }

  include sites-enabled/*;
}
默认值

server {
  listen  80;
  root    /var/www/dist;
  index   index.html index.htm;

  location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires 1d;
  }

  location @proxy {
    proxy_set_header    Upgrade $http_upgrade;
    proxy_set_header    Connection "upgrade";
    proxy_set_header    Host $host;
    proxy_set_header    X-Real-IP $remote_addr;
    proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header    X-NginX-Proxy true;

    proxy_http_version  1.1;
    proxy_redirect      off;
    proxy_pass          http://node-app;
    proxy_cache_bypass  $http_upgrade;
  }

  location / {
    try_files $uri $uri/ @proxy;
  }

}
我期望nginx作为反向代理的所有功能都在那里,除了它提供了前面提到的404无法获得的功能。在四处寻找解决方案后,我试图添加

if (!-e $request_filename){
  rewrite ^(.*)$ /index.html break;
} 
位置/
块中。这允许我刷新并直接访问作为顶级位置的路由,但现在我不能提交PUT/POST请求,而是返回一个不允许的405方法。我可以看到请求没有得到正确处理,因为我添加的配置现在将我的所有请求重写为/index.html,这就是我的API接收所有请求的地方,但我不知道如何将我的PUT/POST请求提交到正确的资源,以及能够刷新和访问我的路线使用反应路由器的HistoryLocation