首页 > 解决方案 > 跨域请求阻塞端口:80

问题描述

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

我认为 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, () => { console.log( Server running on port ${PORT}); });

标签: node.jsnginxcors

解决方案


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

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

推荐阅读