拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 为什么这个NGINX组态档无效?

为什么这个NGINX组态档无效?

白鹭 - 2022-01-23 2162 0 0

所以我有这个 NGINX 组态档:

events { }
http {
    server {
        listen 443 ssl;
        ssl_certificate /home/dietpi/certs/cert.pem;
        ssl_certificate_key /home/dietpi/certs/privkey.pem.key;
        location / {
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass         http://localhost:3001/;
            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection "upgrade";
        }
    }
}

它是 Docker 容器的反向代理。它为我做 SSL。我想将 HTTP 重定向到 HTTPS,所以我尝试添加以下内容:

events { }
http {
    server {
        listen 80 default_server;
        server _;
        return 301 https://$host$request_uri;
    }
    server {
        listen 443 ssl;
        ssl_certificate /home/dietpi/certs/cert.pem;
        ssl_certificate_key /home/dietpi/certs/privkey.pem.key;
        location / {
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass         http://localhost:3001/;
            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection "upgrade";
        }
    }
}

但现在它不起作用(sudo nginx -t失败)。它是这样说的:

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:5
nginx: configuration file /etc/nginx/nginx.conf test failed

大多数遇到此错误的人往往http会将服务器块放在一个块中,而将其放在一个块中可以修复它。但是……我的server街区已经在http街区中了。那么我该如何解决呢?

uj5u.com热心网友回复:

错别字server而不是server_nameserver {}块内

server {
  listen 80 default_server;
  server_name _;   # ? corrected
}
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *