Program Tip

React- 라우터와 nginx

programtip 2020. 10. 28. 20:33
반응형

React- 라우터와 nginx


내 반응 앱을 webpack-dev-server에서 nginx로 전환하고 있습니다.

루트 URL "localhost : 8080 / login"으로 이동하면 간단히 404가 표시되고 내 nginx 로그에서 다음을 가져 오려고합니다.

my-nginx-container | 2017/05/12 21:07:01 [error] 6#6: *11 open() "/wwwroot/login" failed (2: No such file or directory), client: 172.20.0.1, server: , request: "GET /login HTTP/1.1", host: "localhost:8080"
my-nginx-container | 172.20.0.1 - - [12/May/2017:21:07:01 +0000] "GET /login HTTP/1.1" 404 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0" "-"

수정 사항을 어디에서 찾아야합니까?

반응의 내 라우터 비트는 다음과 같습니다.

render(

  <Provider store={store}>
    <MuiThemeProvider>
      <BrowserRouter history={history}>
        <div>
          Hello there p
          <Route path="/login" component={Login} />
          <App>

            <Route path="/albums" component={Albums}/>

            <Photos>
              <Route path="/photos" component={SearchPhotos}/>
            </Photos>
            <div></div>
            <Catalogs>
              <Route path="/catalogs/list" component={CatalogList}/>
              <Route path="/catalogs/new" component={NewCatalog}/>
              <Route path="/catalogs/:id/photos/" component={CatalogPhotos}/>
              <Route path="/catalogs/:id/photos/:photoId/card" component={PhotoCard}/>
            </Catalogs>
          </App>
        </div>
      </BrowserRouter>
    </MuiThemeProvider>
  </Provider>, app);

그리고 내 nginx 파일은 다음과 같습니다.

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 {
        listen 8080;
        root /wwwroot;

        location / {
            root /wwwroot;
            index index.html;

            try_files $uri $uri/ /wwwroot/index.html;
        }


    }
}

편집하다:

로그인하지 않고 localhost : 8080으로 이동하면 로그인 페이지도 표시되기 때문에 대부분의 설정이 작동한다는 것을 알고 있습니다. 이것은 localhost : 8080 / login으로의 리디렉션을 통한 것이 아닙니다. 일부 반응 코드입니다.


nginx 구성의 위치 블록은 다음과 같아야합니다.

location / {
  try_files $uri /index.html;
}

문제는 index.html 파일에 대한 요청이 작동하지만 현재 nginx에게 다른 요청을 index.html 파일로 전달하도록 지시하지 않는다는 것입니다.


내 솔루션은 다음과 같습니다.

간단한 시도 :

location / {
    root /var/www/mysite;
    try_files $uri /index.html;
}

더 이상 비 html 유형을 시도하지 마십시오.

location / {
    root /var/www/mysite;
    set $fallback_file /index.html;
    if ($http_accept !~ text/html) {
        set $fallback_file /null;
    }
    try_files $uri $fallback_file;
}

no more trying for non-html types and directory (making try_files compatible with index and/or autoindex):

location / {
    root /var/www/mysite;
    index index.html;
    autoindex on;
    set $fallback_file /index.html;
    if ($http_accept !~ text/html) {
        set $fallback_file /null;
    }
    if ($uri ~ /$) {
        set $fallback_file /null;
    }
    try_files $uri $fallback_file;
}

Here is the solution that works for me in the live server:

I have just followed this tutorial and with the basic Nginx configuration simply just configured the location block.

location / {
    try_files $uri $uri/ /index.html;
}

참고URL : https://stackoverflow.com/questions/43951720/react-router-and-nginx

반응형