【问题标题】:NGINX: What is wrong with my nginx configurationNGINX:我的 nginx 配置有什么问题
【发布时间】:2018-02-02 13:36:38
【问题描述】:

我有一个由 nginx 提供服务的 Angular 应用程序,这个 Angular 应用程序与同一服务器上的 rest 后端通信。

我的 /etc/nginx/sites-available/ 中有以下两个服务器配置

“默认”和“应用程序” 默认服务器在 80 上监听 Angular 应用程序正确服务。

  server{
    listen 80 default_server;
    listen [::]:80 default_server;


    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {

            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.html ;
    }

}

然后在我的“应用程序”服务器块中,它应该重定向休息后端的请求,

server{
    listen [::]:80 ;

    server_name xxx.com;

    location / {

        proxy_pass http://127.0.0.1:8082;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

}

但是当我的 Angular 应用程序遇到 POST API 端点时,它会得到 405(不允许的方法),这是因为它直接命中 nginx 服务器甚至不会去休息端点(在 8082 上),否则 OPTIONS 将是首先发送,但我只看到一个请求和 POST 直接发出的请求,这意味着它甚至没有重定向到其他端口,否则将在我的 REST 后端调用 CORS 设置。 我不知道我做错了什么,同一个应用程序在本地运行得很好。 我已经仔细检查了启用站点的文件夹中的符号链接,它们是绝对路径。

编辑: 我的请求标头

Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.9
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:example.com
Origin:http://example.com
Pragma:no-cache
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36

我的操作系统 Ubuntu:16.04, nginx:1.10.3。

【问题讨论】:

  • 尝试运行 nginx -t 并发布输出。
  • @advay.umare $ sudo nginx -t nginx:配置文件/etc/nginx/nginx.conf 语法没问题 nginx:配置文件/etc/nginx/nginx.conf 测试成功跨度>
  • 我认为在您的应用服务器块中,您需要添加 8082 来代替 listen [::]:80;
  • 不,这是不正确的,拥有proxy_pass的整个想法是监听80并将其传递给8082,这就是我们使用任何反向代理的方式
  • 是的,但是在 80 上,您正在运行 Angular 应用程序,这导致请求重定向到 Angular 应用程序,而它没有获得其余端点。您在后端使用的是哪个服务器。

标签: angular rest spring-mvc nginx


【解决方案1】:

尝试合并两个服务器块:

 server{
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;
    server_name _;
    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.html ;
    }
    location /api/ {
        proxy_pass http://127.0.0.1:8082/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

并通过在 url 中附加“/api/”来访问所有其余的 api。

可能是它的 CORS 问题,将其添加到您的位置块中:

  if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        # Custom headers and headers various browsers *should* be OK with but aren't
        #
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
            return 204;
  }

【讨论】:

  • @TruckDriver 它可能是 CORS 问题。我已经对我的答案进行了编辑。试试看。