简体   繁体   English

nginx: [emerg] “server”指令在 /etc/nginx/conf.d 中是不允许的,即使服务器标签在 http 中

[英]nginx: [emerg] “server” directive is not allowed here in /etc/nginx/conf.d even if server tag is inside http

I am trying to add CORS header to my app when deploying it to cloud via docker I get the error: nginx: [emerg] "server" directive is not allowed here in /etc/nginx/conf.d/default.conf:1在通过 docker 将其部署到云时,我试图将 CORS 标头添加到我的应用程序中,但出现错误:nginx: [emerg] "server" 指令在 /etc/nginx/conf.d/default.conf:1 中是不允许的

My nginx file我的 nginx 文件

http {
 include       /etc/nginx/mime.types;
 default_type  application/octet-stream;
 server {
      root https://srm-master.nonprod.com;
      index index.html index.htm;
    set $cors "";
    if ($http_origin ~* (.*\.ini.com)) {
      set $cors "true";
    }
    server_name .ini.com;
   location / {
   if ($cors = "true") {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, 
        DELETE, PUT';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'User-Agent,Keep- 
        Alive,Content-Type';
    }
   }
    }

   #gzip  on;

    include /etc/nginx/conf.d/*.conf;
   }

Actually, the problem has nothing to do with Docker , the cause of the error is nginx config.实际上,问题与Docker无关,错误原因是 nginx 配置。 Because nginx allow only one http section, and it has been defined at /etc/nginx/nginx.conf .因为nginx只允许一个http部分,并且它已经在/etc/nginx/nginx.conf定义。 Remove the http section in your config, and it should be worked删除配置中的http部分,它应该可以工作

server {
      root https://srm-master.nonprod.com;
      index index.html index.htm;
    set $cors "";
    if ($http_origin ~* (.*\.ini.com)) {
      set $cors "true";
    }
    server_name .ini.com;
   location / {
   if ($cors = "true") {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, 
        DELETE, PUT';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' 'User-Agent,Keep- 
        Alive,Content-Type';
    }
   }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 nginx: [emerg] "http" 指令在 /etc/nginx/conf.d/default.conf:1 中是不允许的 - nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1 nginx: [emerg] "http" 指令在 /etc/nginx/conf.d/site.conf:1 中是不允许的 - nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/site.conf:1 错误 - nginx: [emerg] "server" 指令在 /etc/nginx/nginx.conf:1 中是不允许的 - Error - nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:1 nginx:在 /etc/nginx/conf.d/nginx.conf:12 的上游“php”中找不到 [emerg] 主机 - nginx: [emerg] host not found in upstream "php" in /etc/nginx/conf.d/nginx.conf:12 nginx:[emerg] 在 /etc/nginx/conf.d/default.conf 的上游“api:5000”中找不到主机 - nginx: [emerg] host not found in upstream "api:5000" in /etc/nginx/conf.d/default.conf AWS ECR,基于 ECS 的部署:nginx:[emerg] 主机未在上游“app:9000”中找到,位于 /etc/nginx/conf.d/default.conf:2 - AWS ECR, ECS based deployment: nginx: [emerg] host not found in upstream "app:9000" in /etc/nginx/conf.d/default.conf:2 Docker 撰写错误:nginx:在 /etc/nginx/conf.d/default.conf:21 的上游“应用程序”中找不到 [emerg] 主机 - Docker compose error: nginx: [emerg] host not found in upstream “app” in /etc/nginx/conf.d/default.conf:21 docker-compose up由于COPY conf.d / * /etc/nginx/conf.d无法正常工作而失败 - docker-compose up is failing becuase of COPY conf.d/* /etc/nginx/conf.d is not working docker (windows10 wsl2) 无法挂载。/nginx/conf.d:/etc/nginx/conf.d - docker (windows10 wsl2) unable to mount ./nginx/conf.d:/etc/nginx/conf.d docker 运行命令结束于:nginx:[emerg] 未知指令“events” in /etc/nginx/nginx.conf:1 - docker run command ends up in : nginx: [emerg] unknown directive “events” in /etc/nginx/nginx.conf:1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM