使用Nginx Docker部署React应用时如何使路由正常工作

时间:2019-09-18 14:12:57

标签: docker nginx

尝试使用nginx docker部署react应用,但无法使子文件夹正常工作。在这里已经阅读了非常相似案例的所有建议,但仍然没有结果。我有带有自定义配置和端口映射9999:80运行的nginx的docker-compose容器。尝试直接访问任何子文件夹时,我从Nginx获取404。附加我的nginx配置。

尝试获取/ statistics子文件夹时,nginx容器的日志中包含什么:


frontend_1  | 172.21.0.1 - - [18/Sep/2019:14:01:27 +0000] "GET /statistics HTTP/1.1" 404 556 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" "-"
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    include /etc/nginx/conf.d/*.conf;

    server {
        root /usr/share/nginx/html;
        location / {
            try_files $uri /index.html;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

就我而言,它有助于将侦听端口从defaut 80更改为8080

server {
    listen 8080;
    root /usr/share/nginx/html;
    location / {
        try_files $uri $uri/ /index.html;
    }
}