我有一个角应用程序由NGIX服务,这个角度应用程序在同一服务器上与一个休息后端对话。
我的/etc/nginx/sites中有以下两个服务器配置/
“默认”和“应用”
默认服务器监听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 ;
    }

}
然后在我的“app”服务器块中,它应该重定向rest后端的请求,
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服务器,甚至不会到达rest端点(在8082上),否则会先发送一个选项,但我只看到一个请求被发出,post直接发出,这意味着它甚至不会重定向到其他端口否则将在我的rest后端调用cors设置。
我不知道我做错了什么,同样的应用程序在本地工作得很好。
我已经检查了站点启用文件夹中的Syrink,它们是绝对路径。
编辑:
我的请求头
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

我的操作系统
乌班图:16.04,
nginx:1.10.3。

最佳答案

尝试合并两个服务器块:

 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;
  }

07-27 13:37