[英]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.