reactjs和nginx服务

时间:2019-03-13 20:39:22

标签: reactjs nginx nginx-config

我正在通过nginx服务我的reactjs构建,并且在我重新加载页面或重新加载窗口之前,它工作正常。在这种情况下,我会收到一些错误消息,我认为这与我配置nginx的方式有关。下图显示了应该从url / path1 / static请求的静态资源。我认为问题在于请求的静态路径错误。下面也是我的nginx配置。如何更改我的nginx,以便静态文件夹始终使用绝对路径而不是相对路径。非常感谢!

在请求网址中,蓝色框是path1,红色是我的域网址。

# auto detects a good number of processes to run
worker_processes auto;

#Provides the configuration file context in which the directives that affect connection processing are specified.
events {
    # Sets the maximum number of simultaneous connections that can be opened by a worker process.
    worker_connections 8000;
    # Tells the worker to accept multiple connections at a time
    multi_accept on;
}


http {
    # what times to include
    include       /etc/nginx/mime.types;
    # what is the default one
    default_type  application/octet-stream;

    # Sets the path, format, and configuration for a buffered log write
    log_format compression '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $upstream_addr '
        '"$http_referer" "$http_user_agent"';

    server {
        # listen on port 80
        listen 80;
        # save logs here
        access_log /var/log/nginx/access.log compression;

        # where the root here
        root /var/www/html/build;
        # what file to server as index
        index index.html;

        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to redirecting to index.html
            try_files $uri /index.html;
        }

        # Media: images, icons, video, audio, HTC
        location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
          expires 1M;
          access_log off;
          add_header Cache-Control "public";
        }

        # Javascript and CSS files
        location ~* \.(?:css|js)$ {
            root /var/www/html/build/;
        }

        # Any route containing a file extension (e.g. /devicesfile.js)
        location ~ ^.+\..+$ {
            try_files $uri =404;
        }
    }
}

enter image description here

0 个答案:

没有答案