add header Access-Control-Allow-Headers via nginx
09:00 01 Apr 2022

How to insert header in response request via nginx? The fact is that a CORS error occurs during a cross-domain request, which is solved simply by adding the Access-Control-Allow-Headers header through the nginx proxy server (as I read on the Internet). Here is cors error code: enter image description here Backend I can't change and disable cors check.

  1. No headers are connected, although the application is used and works correctly. nginx config:

server {
  listen 80;

  add_header Access-Control-Allow-Headers "*" always;
  add_header Access-Control-Allow-Methods "*" always;
  add_header Access-Control-Allow-Origin "*" always;
  add_header Custom header value;
  
  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri//index.html;
  }

  error_page 500 502 503 504 /50x.html;

  location = /50x.html {
    root /usr/share/nginx/html;
  }
}

  1. How the request works. Created a simple fetch on a backup that contains one controller. Simple get request with React:

function MyPoster() {
      const [recommended, setRecommended] = useState(null);
     
      constant getPeers = () =>
        fetch('http://localhost:9002/c', {method: "GET"})
          .then(response => response.text());
     
      useEffect(() => {
        get peers()
          .then(result => {
            setRecommended(result);
          });
      }, [])
     
      return (
        
{recommended}
); }

  1. The backup controller is also simple, which returns the results.

@GetMapping("/s")
public string simpleTemplate2() {
  return "I'm a programmer";
}

  1. Docker configuration

FROM node: 12.2.0-alpine as react_build
WORKING DIRECTORY/application
COPY . /Appendix/

#prepare the container for building react
RUN npm install --silent
RUN npm install react-scripts@3.0.1 -g --silent
RUN npm run build

#preparing nginx
FROM nginx: 1.16.0-alpine

COPY --from=react_build /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
copy nginx/nginx.conf /etc/nginx/conf.d

#start nginx
EXPOSURE 80
CMD ["nginx","-g","daemon off;"]

https://github.com/MarkSinD/docker-nginx-react Why are the headings not coming up? What can be added?

reactjs docker nginx