【问题标题】:Cross origin request blocked port :80跨域请求阻塞端口:80
【发布时间】:2021-11-12 12:34:15
【问题描述】:

我问了一个“朋友”(顺便说一句,他没有接听我的电话)为我建立了一个运行良好的网站,直到我从 LetsEncrypt 安装了 SSL 并添加了 cloudflare 保护。 我不断收到此错误,请参阅打印屏幕: print screen

我认为 node.js 正在尝试连接到端口:80,这是一个 http 端口而不是 https。 nginx 使用端口 443。 (当我关闭 cloudflare 时,网站又开始工作了) 根据我的阅读,我需要一个 nodejs 的反向代理,但不知道添加什么以及在哪里添加。 我的站点可用/默认如下所示:

#   listen 80;
#       listen 443 ssl; # managed by Certbot
#   server_name wagthro.com www.wagthro.com;
#       ssl_certificate /etc/letsencrypt/live/wagthro.com/fullchain.pem; # mana>
#       ssl_certificate_key /etc/letsencrypt/live/wagthro.com/privkey.pem; # ma>
#        
#   return 301 https://wagthro.com$request_uri;
#}

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

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    # root /var/www/html;
    root /var/www/WeAreGoingToHellRo/client/build;

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

    server_name wagthro.com www.wagthro.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        #try_files $uri $uri/ =404;
        if (!-e $request_filename){
                        rewrite ^(.*)$ /index.html break;
                }
    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/wagthro.com/fullchain.pem; # mana>
        ssl_certificate_key /etc/letsencrypt/live/wagthro.com/privkey.pem; # ma>
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

谢谢!

乐:

如何找到端口?我尝试了端口:8080、8000、3000、3005、9229、5000,但没有任何效果。

我的 nginx 现在看起来像这样:

# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
#server {
#   listen 80;
#       listen 443 ssl; # managed by Certbot
#   server_name wagthro.com www.wagthro.com;
#       ssl_certificate /etc/letsencrypt/live/wagthro.com/fullchain.pem; # mana>
#       ssl_certificate_key /etc/letsencrypt/live/wagthro.com/privkey.pem; # ma>
#        
#   return 301 https://wagthro.com$request_uri;
#}

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

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    # root /var/www/html;
    root /var/www/WeAreGoingToHellRo/client/build;

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

    server_name wagthro.com www.wagthro.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        #try_files $uri $uri/ =404;
        if (!-e $request_filename){
                        rewrite ^(.*)$ /index.html break;
                }
          location / {
        proxy_pass http://127.0.0.1:9229;
        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;
               }

    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/wagthro.com/fullchain.pem; # mana>
        ssl_certificate_key /etc/letsencrypt/live/wagthro.com/privkey.pem; # ma>
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

我在节点服务器文件中得到了这个:server.listen(PORT, () => { 控制台.log(Server running on port ${PORT}); });

【问题讨论】:

  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: node.js nginx cors


【解决方案1】:

错误是关于 CROSS ORIGIN POLICY 的。从 npm 安装 CORS 包并在你的节点服务器中使用它:

const express = require('express')
const cors = require('cors')
const app = express()
app.use(cors())

【讨论】:

  • 我明白了。但是为什么要尝试连接到端口:80?我不应该需要一个nodejs的反向代理,像这样吗? const http = require('http'); http.createServer((req,res) => { res.writeHead(200, {"Content-Type":"plain/html"}); res.end('Node is Running'); }).listen(3000);
  • 您没有将连接重定向到您的节点服务器。在 NGINX 服务器文件中使用 LOCATION。 LOCATION 应该指向您在本地运行的节点服务器。
  • 所以我应该添加这个块location / { proxy_pass http://localhost:80; 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; } }
  • 我添加了这个。用 sudo nginx -t 验证,没问题,成功重启 nginx 现在我得到错误 502 错误。可能节点的端口不是80。
  • 你的 Node 服务器不应该监听 80 端口,你的 NGINX 应该。这个想法是您的 NGINX 充当客户端和 Node 服务器之间的代理,因此 Node 服务器永远不会暴露在互联网上,NGINX 会。您可以在任意端口(如 3000)上运行 Node,然后通过将 LOCATION 设置为“localhost:3000”来设置 NGINX 以将连接重定向到该端口。
猜你喜欢
  • 1970-01-01
  • 2015-06-05
  • 1970-01-01
  • 2019-01-22
  • 2020-02-10
  • 2019-10-09
  • 1970-01-01
  • 2021-06-26
相关资源
最近更新 更多