欢迎访问 React 容器化应用的 nginx 页面

huangapple go评论70阅读模式
英文:

Welcome to nginx page on React dockerized app

问题

I see that you have a React app running in a Docker container with an Nginx server, and you're encountering the Nginx welcome page instead of your app. It seems like a routing issue. Here are a few steps to check:

  1. Ensure that the Docker image is built correctly and the React app's build files are copied to the Nginx serving directory.
  2. Make sure that your React app's HTML file (usually index.html) includes the appropriate relative paths to CSS and JavaScript files.
  3. Check your Nginx configuration. It appears to be set up to serve from /usr/share/nginx/html, which is the default Nginx root directory.
  4. Verify that your React app's routes are configured correctly. Ensure that the routes in your React app match the ones defined in your Nginx configuration.
  5. Double-check that there are no typos or errors in your Nginx configuration file.

If you've already tried these steps, and the issue persists, it might be helpful to check the Nginx error logs for any specific error messages that could provide more insight into the problem. You can access the Nginx error logs by running:

docker logs <container_id>

Replace <container_id> with the actual ID of your running Docker container. This should give you more information about what's going wrong with the Nginx setup.

Remember to restart your Docker container after making any changes to the Nginx configuration or the React app's build.

英文:

I created a React app and oused react-router for the routes. When I serve the app using npm start, it works perfectly. But when I dockerize it and use nginx, all I get when executing the app is the "Welcomer to nginx!" page.
This is how I handle the routes with react-router:

function WallieRoutes() {

  return (
    &lt;BrowserRouter&gt;
      &lt;Routes&gt;
        &lt;Route path={Locations.DEFAULT} element={&lt;Landing /&gt;} /&gt;
        &lt;Route path={Locations.LOGIN} element={&lt;Login /&gt;} /&gt;
        &lt;Route path={Locations.SIGN_UP} element={&lt;SignUp /&gt;} /&gt;
        &lt;Route path={Locations.VALIDATE} element={&lt;ValidateUser /&gt;} /&gt;
        &lt;Route path={Locations.VERIFICATE} element={&lt;VerificateCode /&gt;} /&gt;
        &lt;Route path={Locations.FORGOTPASSWORD} element={&lt;ForgotPassword /&gt;} /&gt;
        &lt;Route path={Locations.RESETPASSWORD} element={&lt;ResetPassword /&gt;} /&gt;
        &lt;Route path={Locations.HOME} element={
          &lt;PrivateRoute&gt;
            &lt;Homepage /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.EXPENSES} element={
          &lt;PrivateRoute&gt;
            &lt;Expenses /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.INCOMES} element={
          &lt;PrivateRoute&gt;
            &lt;Incomes /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.INVESTMENTS} element={
          &lt;PrivateRoute&gt;
            &lt;Investments /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.CONFIGURATION} element={
          &lt;PrivateRoute&gt;
            &lt;Configuration /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.FINANCIAL_PROFILE} element={
          &lt;PrivateRoute&gt;
            &lt;FinancialProfile /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.CARDS} element={
          &lt;PrivateRoute&gt;
            &lt;Cards /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.SUBSCRIPTION} element={
          &lt;PrivateRoute&gt;
            &lt;Subscription /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.SUBSCRIPTION_PROCESS} element={
          &lt;PrivateRoute&gt;
            &lt;SubscriptionProcess /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.PREEXISTING_EXPENSES} element={
          &lt;PrivateRoute&gt;
            &lt;PreexistingExpenses /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.ASK_WALLIE} element={
          &lt;PrivateRoute&gt;
            &lt;AskWallie /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.GROUPS} element={
          &lt;PrivateRoute&gt;
            &lt;Groups /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.CALENDAR} element={
          &lt;PrivateRoute&gt;
            &lt;ExpenseCalendar /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.CASH_OR_CREDIT} element={
          &lt;PrivateRoute&gt;
            &lt;CashOrCredit /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path={Locations.INVESTMENTS_LANDING} element={
          &lt;PrivateRoute&gt;
            &lt;InvestmentsLanding /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        {/* &lt;Route path={Locations.USER_INVESTMENT} element={
          &lt;PrivateRoute&gt;
            &lt;UserInvestments /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt; */}
        &lt;Route path=&quot;*&quot; element={&lt;NotFound /&gt;} /&gt;
        &lt;Route path={Locations.PROJECTIONS} element={
          &lt;PrivateRoute&gt;
            &lt;Projections /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path=&quot;*&quot; element={&lt;NotFound /&gt;} /&gt;
        &lt;Route path={Locations.EXCHANGE_RATES} element={
          &lt;PrivateRoute&gt;
            &lt;ExchangeRates /&gt;
          &lt;/PrivateRoute&gt;
        } /&gt;
        &lt;Route path=&quot;*&quot; element={&lt;NotFound /&gt;} /&gt;
      &lt;/Routes&gt;
    &lt;/BrowserRouter&gt;
  );
}

This is my Docker file:

FROM node:16.17.0 AS build

WORKDIR /app

COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx:1.19

COPY ./nginx/nginx.conf /etc/nginx/nginx.conf
COPY --from=build /app/build /user/share/nginx/html

And this is my nginx/nginx.conf file:

worker_processes  1;

events {
  worker_connections  1024;
}

http {
  server {
    listen 80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html index.htm;
    include /etc/nginx/mime.types;

    gzip on;
    gzip_min_length 1000;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

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

This is what I see when I run the app with Docker:
欢迎访问 React 容器化应用的 nginx 页面

Any ideas on what could be wrong?

  • I've already googled the issue and tried multiple nginx configurations but I still get the same nginx welcome page.

答案1

得分: 1

解决! 我通过在Dockerfile中将user更改为usr来解决了它。
这是我的Dockerfile现在的样子:

FROM node:16.17.0 作为构建

WORKDIR /app

COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx:1.19

COPY ./nginx/nginx.conf /etc/nginx/nginx.conf
COPY --from=build /app/build /usr/share/nginx/html
英文:

Solved! I solved it by changing user to usr in the Dockerfile.
This is my Dockerfile now:

FROM node:16.17.0 AS build

WORKDIR /app

COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx:1.19

COPY ./nginx/nginx.conf /etc/nginx/nginx.conf
COPY --from=build /app/build /usr/share/nginx/html

huangapple
  • 本文由 发表于 2023年4月17日 21:13:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定