Nginx上的Node应用正在下载index.html而不是对其进行更新

时间:2018-06-27 13:57:06

标签: node.js http ssl nginx

首先,这与php无关。

我遵循了this教程,将一个小型节点应用程序部署到了digitalocean。我的应用程序中有一个按钮,它可以获取一些文本,然后使用该按钮更新页面上的div。在实时nginx服务器上运行时会发生问题;该按钮会使浏览器下载该应用程序的index.html,而不仅仅是更新页面。在本地主机上测试时,我无法做到这一点,这意味着它与nginx配置有关。当我查找此问题时,所有解决方案都围绕着服务php文件。我的/etc/nginx/sites-enabled/default就像这样

upstream node-upstream {
    server MY_IP_ADDRESS:5000;
}

server {
    listen 80 default server;
    listen [::]:80 default_server ipv6only=on;
    return 301 https://$host$request_uri;

    server_name <my_site>.com www.<my_site>.com;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2 ipv6only=on;

    root /path/to/app;
    index index.html;

    server_name <my_site>.com www.<my_site>.com;

    ssl_certificate /etc/letsencrypt/live/<my_site>.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/<my_site>.com/privkey.pem;

    include snippets/ssl-params.conf;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:5000;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
    }
}

编辑::当我使用curl -v https://<my_site>.com/时,它还会在终端中输出整个index.html,但在显示此内容后立即显示

* Rebuilt URL to: https://<my_site>.com/
*   Trying <IPV6_ADDRESS>...
* TCP_NODELAY set
* Connected to <my_site>.com (<IPV6_ADDRESS>) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /anaconda3/ssl/cacert.pem
  CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: CN=<my_site>.com
*  start date: Jun 27 05:34:01 2018 GMT
*  expire date: Sep 25 05:34:01 2018 GMT
*  subjectAltName: host "<my_site>.com" matched cert's "<my_site>.com"
*  issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
*  SSL certificate verify ok.
> GET / HTTP/1.1
> Host: <my_site>.com
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.14.0
< Date: Wed, 27 Jun 2018 21:31:14 GMT
< Content-Type: text/html; charset=UTF-8
< Content-Length: 3030
< Connection: keep-alive
< X-Powered-By: Express
< Accept-Ranges: bytes
< Cache-Control: public, max-age=0
< Last-Modified: Wed, 27 Jun 2018 12:56:18 GMT
< ETag: W/"bd6-1644150e6fc"
< Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
< X-Frame-Options: DENY
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
<

节点应用程序中负责更新的代码是这样的:

const express = require('express');
const router = express.Router();

// serve the index.html
router.get('/', (req, res) => {
  res.setHeader('Content-Type', 'text/html');
  let file_contents = fs.readFileSync("./public/index.html", {encoding: "utf8"});
  res.write(file_contents);
  res.end();
});

// grab the text and reload the index.html
router.post('/', (req, res) => {
  req.on("data", function(input_value) {
    let query = input_value.toString();
    let num_paragraphs = query.split("=").pop();
    let hi_text = my_db.getAllParagraphs(num_paragraphs);
    let file_contents = fs.readFileSync("./public/index.html", {encoding: "utf8"});
    file_contents = file_contents.replace("<span class='hit'></span>", hi_text);
    res.write(file_contents);
    res.end();
  });
});

module.exports = router;

1 个答案:

答案 0 :(得分:-1)

您也可以发布节点端代码吗 您的配置似乎正确