React,Nginx,Docker和Sping Boot ,GET请求工作,但POST/PUT/DELETE返回HTTP 403 Postman无缝工作

tvmytwxo  于 2023-08-03  发布在  Nginx
关注(0)|答案(1)|浏览(103)

我试图部署我的webapp在我的域,一切似乎工作的时刻,但最后几个步骤。
当我运行我的前端时,它确实成功地运行了“GET”请求,但是当我想编辑,创建或删除一些东西时,我的后端返回HTTP:403禁止。
我不知道为什么。使用Postman,所有调用都成功,只是使用我的webapp返回失败的请求
下面是我的nginx.conf:

`server {
    listen 80 default_server;
    server_name DOMAIN.com;

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

    location /urs {
        rewrite /api/(.*) /$1  break;
        proxy_pass http://service:9001;
        proxy_pass_request_headers on;
        default_type  application/json;
    }
}`

字符串
下面是我docker-composed文件:

`version: '3.8'
services:
  frontend:
    stdin_open: true
    image: DOMAIN-client
    container_name: DOMAIN-client-c
    build:
      context: ./UniversalReservationSystemFrontend
      dockerfile: Dockerfile
    ports:
      - "80:80"
    environment:
      - NODE_ENV=production
    volumes:
      - /etc/nginx/certificates:/etc/nginx/certificates
  postgres:
    image: postgres
    container_name: psql-db-c
    environment:
      POSTGRES_DB: xx
      POSTGRES_USER: xx
      POSTGRES_PASSWORD: xx
    ports:
      - "5432:5432"
    volumes:
      - ./UniversalReservationSystem/src/main/resources/database/schema.sql:/docker-entrypoint-initdb.d/schema.sql:ro
  service:
    container_name: DOMAIN-system-c
    build:
      context: ./UniversalReservationSystem
      dockerfile: Dockerfile
    ports:
      - "9001:9001"
    environment:
      URS_DATABASE_URL: r2dbc:postgresql://postgres:5432/xx
      URS_DATABASE_USERNAME: xx
      URS_DATABASE_PASSWORD: xx
    depends_on:
      - postgres`


和我的React应用的Dockerfile:

`# pull official base image
FROM node:20 AS builder

# Set the working directory to /client inside the container where the UI will be located at
# as if we would use "cd" in linux, if the folder does not exist, it will create the folder under the given path
WORKDIR /usr/src/client

# Copy app files to workdir inside the docker container
COPY package*.json ./

# Install dependencies (npm ci makes sure the exact versions in the lockfile gets installed)
RUN npm ci

# Copy all of the project files into the docker container
COPY . .

# RUN the build after installing dependencies and the "COPY" of the whole source code
RUN npm run build

##########################
# Use the official Nginx base image with Alpine Linux as the base image for the final production container.
# Nginx will be used to serve the static files generated by the React app.
FROM nginx:alpine

# Set the working directory inside the container to /usr/share/nginx/html, where Nginx serves the static content.
WORKDIR /usr/share/nginx/html

# Remove the default Nginx configuration file that comes with the image.
# We will replace it with our custom configuration file.
RUN rm /etc/nginx/conf.d/default.conf

# Copy the custom Nginx configuration file from the host (your machine) to the container.
# This configuration file will be used by Nginx to serve the React app and handle incoming requests.
COPY conf/nginx.conf /etc/nginx/conf.d/default.conf

# Remove all the existing files in the /usr/share/nginx/html directory to clear any default content that Nginx might have placed there.
RUN rm -rf ./*

# Copy the production build files of the React app (generated in the previous build stage) from the "builder" stage into the /usr/share/nginx/html directory.
# This makes the built React app available to Nginx for serving.
COPY --from=builder /usr/src/client/build .

# Set the default command that will run when the container starts.
# This command starts Nginx in the foreground to serve the React app and keeps the container running.
ENTRYPOINT ["nginx", "-g", "daemon off;"]
`


部分Docker日志
DOMAIN-client-c|79.230.89.194- - [26/Jul/2023:22:27:49 +0000]“POST /urs/tenant HTTP/1.1”200 98“-”“PostmanRuntime/7.32.3”“-”DOMAIN-system-c| 2023-07-26T22:28:02.176Z INFO 1 - [ parallel-4] d.g.u.r.s.a.i.RequestIdHandler:API调用:POST http://service:9001/urs/tenant DOMAIN-client-c|192.168.32.1- - [26/Jul/2023:22:28:02 +0000]“POST /urs/tenant HTTP/1.1”403 0“http://MY-DOMAIN.com/administration/tenant/create““Mozilla/5.0(Windows NT 10.0; Win64; x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/115.0.0.0 Safari/537.36””-”
在这里,您可以看到使用“Postman”的第一个调用使用“POST”API调用确实可以工作。但是在webapp上使用相同的调用并不起作用。
任何帮助都是感激的。
我已经尝试了几个配置和视频,也了解背后发生了什么,并试图修复它。以及检查我的CORS设置在我的后端,没有这些想法/建议成功:(

k5hmc34c

k5hmc34c1#

解决方案是,即使认为“nginx”访问通过.“root用户”(ofc.仅用于测试目的),他没有权限访问我的前端目录。
加入:

# Test giving access to all users
RUN chmod -R a+rx .

字符串
我的Dockerfile解决了这个问题。
小编辑:Ofc。我将添加一个用户与正确的访问为下一次,运行它的“根”是前面说过的,只是为了测试的目的

相关问题