簡體   English   中英

如果 url 中缺少尾部斜杠,nginx 不會將請求傳遞給節點服務器

[英]nginx not passing request to node server, if trailing slash is missing in the url

我已經用 wordpress 設置了 nginx,它工作正常。 現在我創建了一個反應應用程序,它在端口 3000 中運行。如果某個位置匹配,我希望我的 nginx 服務器將請求傳遞給反應服務器。 下面是 nginx 配置與 wordpress 和反應應用程序。

    listen 80;
    server_name aaroogya.org;
    return 301 https://aaroogya.org$request_uri;
}
server {
#        listen 80;

       
    root /var/www/wordpress;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name aaroogya.org www.aaroogya.org;
    #location = /favicon.ico { log_not_found off; access_log off; }
        #location = /robots.txt { log_not_found off; access_log off; allow all; }
    #server_name testbed2.covidhelp.in;

    location /covidhelp{
#root /var/www/;
#   index index.html;
        add_header Access-Control-Allow-Origin http://127.0.0.1:3000/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://127.0.0.1:3000/ ;
                proxy_redirect off;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_redirect off;
                proxy_set_header X-Forwarded-Proto $scheme;
    }




     location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
        }
        location / {
                #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/aaroogya.org/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/aaroogya.org/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


}

當我訪問https://www.aaroogya.org/covidhelp/

它將請求重定向到反應服務器,但是當我嘗試加載所有 static 文件(如 bundle.js)時,它無法正常工作。 例如https://www.aaroogya.org/covidhelp/static/js/main.chunk.js - 不工作的例子https://www.aaroogya.org/covidhelp/static/js/main.chunk.js/ - 添加了一個斜杠,它的工作正常。

我已經通過 2 個步驟解決了這個問題。

  1. 檢查/var/log/nginx/error.log
connect() failed(111: Connection refused) while connecting to upstream, client: * .*.*.*, server: * .*.*.*, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "*.*.*.*"

即使我在 nginx conf 文件中將上游設置為127.0.0.1:3000 ,上游仍然是127.0.0.1:8000

  1. 將服務器 127.0.0.1:8000 替換為/etc/nginx/conf.d/virtual.conf中的服務器127.0.0.1:3000並重新啟動 nginx。

以下:

server {
    listen       80;
    server_name  SERVER_IP_ADDRESS;

    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

然后:

sudo /etc/init.d/nginx restart 

最后,它沒有 502 錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM