通过Nginx for .net核心的反向代理不起作用

时间:2019-12-17 04:55:15

标签: nginx

我已经在Angular中构建了UI,并在.Net核心中构建了API。

我正在使用Nginx将发送到http://localhost:4000/api的请求转发到http://localhost:5000/api

Nginx还在根目录“ /”中提供我的静态文件。

我确定当我直接向http://localhost:5000/api/发送请求时,我的Kestrel服务器正在响应。...

我无法将我的请求代理到http://localhost:5000进行api调用。

非常感谢任何帮助。

这是我的Nginx配置。

worker_processes auto;

events { worker_connections 1024; }

http {

    log_format   main '$remote_addr - $remote_user [$time_local]  $status '
    '"$request" $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile on;



    server {
        listen 80;
        listen 4000;

        server_name  localhost;
        server_name 127.0.0.1;

        gzip                    on;
        gzip_comp_level         6;
        gzip_vary               on;
        gzip_min_length         1000;
        gzip_proxied            any;
        gzip_types              text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_buffers            16 8k;
        client_max_body_size    256M;

        root /usr/share/nginx/html;

        index  index.html index.htm;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        location /api {
            proxy_pass         http://localhost:5000/api/;
            proxy_redirect     off; 
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_set_header   X-Forwarded-Host $server_name;


        }

        location / {
            try_files $uri $uri/ /index.html =404;
        }

    }

}

1 个答案:

答案 0 :(得分:0)

我想您需要按如下方式编写

    location /api/ {
      rewrite            ^/api/(.*)                  /api/$1         break;
      proxy_pass         http://localhost:5000;
      proxy_redirect     off; 
      proxy_set_header   Host $host;
      proxy_set_header   X-Real-IP $remote_addr;
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header   X-Forwarded-Proto $scheme;
      proxy_set_header   X-Forwarded-Host $server_name;
}