nginx服务器块代理传递问题

ycl3bljg  于 2023-04-29  发布在  Nginx
关注(0)|答案(1)|浏览(144)

我有一个节点应用程序在我的Ubuntu服务器上运行,超过4000个端口,带有域示例。然后我在nginx配置中创建了一个服务器块,以后端作为节点运行我的angular应用程序。所以在我的angular应用程序中,我的节点服务器运行在3001之上。我将展示这个的nginx配置

server {
        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;
      server_name example.com www.example.com;

location / {
        #  try_files $uri $uri/ =404;
        proxy_pass http://localhost:4000;
        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;
                client_max_body_size 200M;
                                                        }
location /api/ {
        proxy_pass http://localhost:3001;
        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;
        client_max_body_size 200M;
    }

location /app2 {
    alias /var/www/html/app2;
    try_files $uri$args $uri$args/ /app2/index.html;
  }

location /assets/ {
        alias /var/www/html/app2/assets/;
        expires 1h;
        add_header Cache-Control "public";
        try_files $uri $uri/ =404;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

                listen 80;
                listen [::]:80;
            server_name example.com www.example.com;
    return 404; # managed by Certbot



}

所以我能够访问我的Angular 应用程序超过例子。com/app 2,但我无法使用www. example从我的angular应用程序发出请求 www.example.com 。404.第404章不可能我猜nginx没有将请求转发到我的angular应用程序节点正在运行的3001端口
我已经检查了我的节点服务器是否正在运行。
我从我的服务器上发出了一个curl请求,比如localhost:3001,我得到了很好的响应,但是不能发出一个请求,比如curl www.example.com 网站上。
我在angular app中定义的url是https://example.com/api。例如,如果我尝试向我的节点服务器发出https://example.com/api/login请求,其中我有路由aswww. example www.example.com (“/login”),它将结束在NOT FOUND 404

ncgqoxb0

ncgqoxb01#

您是否尝试过根据文档中解释的原因切换位置
例如,像这样:

server {
        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;
      server_name example.com www.example.com;

                                                        }
location /api/ {
        proxy_pass http://localhost:3001;
        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;
        client_max_body_size 200M;
    }

location /app2 {
    alias /var/www/html/app2;
    try_files $uri$args $uri$args/ /app2/index.html;
  }

location / {
        #  try_files $uri $uri/ =404;
        proxy_pass http://localhost:4000;
        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;
                client_max_body_size 200M;

我认为example.com/api/foo可以匹配您定义的第一个位置,这导致了404
希望这有帮助!

相关问题