【问题标题】:redirect example.com:3000 to example.com将 example.com:3000 重定向到 example.com
【发布时间】:2018-07-05 21:34:58
【问题描述】:

将 (301) https://example.com:3000 重定向到 https://example.com,而 3000 端口只能通过 IP:3000 而不能通过 example.com:3000 访问

  • 在端口 3000 上使用 Express.js 应用程序。
  • 使用 nginx 代理 localhost:3000 和 example.com。
  • 现在https://example.com:3000 无法访问(在 chrome 中: ERR_CONNECTION_CLOSED),但 IP:3000 可以访问。
  • 问题是 - 搜索引擎索引几乎所有 https://example.com:3000 页面,这些页面无法访问。

由于 nodejs 已经占用了 3000 端口,在 nginx 中我无法写入:

server {
    listen 3000;
    server_name example.com;
    return 301 https://example.com$request_uri;
}

nginx 配置:

upstream nodejs {
    ip_hash;
    server localhost:3000;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com;
    return 301 https://$server_name$request_uri;
}

server {

    listen 443 ssl  default_server;
    server_name example.com;

    listen [::]:443 ssl  default_server;

    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;

    include /etc/nginx/snippets/letsencrypt-acme-challenge.conf;


    location = /robots.txt {
        root /root;
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~* \.(?:css|js)$ {
        root /root;

        expires 9d;

        add_header Cache-Control "public, max-age=7200";
        add_header X-Content-Type-Options nosniff;
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Robots-Tag none;
        add_header X-Download-Options noopen;
        add_header X-Permitted-Cross-Domain-Policies none;
        # Optional: Don't log access to assets
        access_log off;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|flv|swf)$
    {
        root /root;    
        expires 365d;
        access_log off;
    }

    # @nodejs
    location / {

        add_header Cache-Control "private";
        add_header Vary "Cookie, User-Agent";

        proxy_redirect off;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_cache_bypass $http_upgrade; 

        include /etc/nginx/proxy_params;

        proxy_pass http://nodejs;
    }
}

如何https://example.com:3000 => https://example.com 并限制外部访问 3000 端口(仅保留 localhost:3000)?

【问题讨论】:

  • 在端口 :3001 上启动节点并让 nginx 在 :3000 上侦听。然后将流量从 :3000 重定向到 :80 和代理 :80 到 :3001 ;-) 请将您的问题标题更改为可读的内容:-S
  • @DanFromGermany:我做到了。现在 http://example.com:3000 重定向(当我使用邮递员或这个 tool,但当我使用 Chrome 时不会,因为 Chrome 会自动将 url 重写为 https),但是 https://example.com 没有。
  • 因为您过去发送了一个 HSTS 标头

标签: node.js redirect nginx


【解决方案1】:

再添加一个服务器块,如下所示:

server {
    listen EXTERNAL_IP:3000 ;
    server_name example.com;

    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;
    include /etc/nginx/snippets/letsencrypt-acme-challenge.conf;    

    return 301 https://$server_name$request_uri;
}

请注意,该应用程序应该只监听 127.0.0.1:3000,否则您可能会遇到“地址已在使用中”。

在这种情况下,所有传入连接都将通过 nginx 建立,从而根据您的规则重定向用户。

如果您想限制对端口 3000 的访问,您可以使用任何防火墙。 iptables 示例:

iptables -I INPUT -p tcp -i eth1 --dport 3000 -j DROP

但这也会关闭对https://example.com:3000的访问。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 2011-12-13
    • 2011-02-28
    • 2014-10-17
    • 2015-05-21
    • 1970-01-01
    相关资源
    最近更新 更多