IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    http协议下的express接口正常,改为https协议接口就会404

    alloyteamzy发表于 2017-06-04 10:42:25
    love 0

    直接上代码,express接口的入口js,index.js:

    const userApi = require(’./api/userApi’); const fs = require(‘fs’); const path = require(‘path’); const bodyParser = require(‘body-parser’); const express = require(‘express’); var app = require(‘express’)(); var https = require(‘https’); var privateKey = fs.readFileSync(‘cert/214123970110593.key’, ‘utf8’); var certificate = fs.readFileSync(‘cert/214123970110593.pem’, ‘utf8’); var credentials = {key: privateKey, cert: certificate}; var httpsServer = https.createServer(credentials, app); app.all(’’, function(req, res, next) { res.header(“Access-Control-Allow-Origin”, ""); res.header(“Access-Control-Allow-Headers”, “X-Requested-With”); res.header(“Access-Control-Allow-Methods”, “PUT,POST,GET,DELETE,OPTIONS”); res.header(“X-Powered-By”, ’ 3.2.1’) res.header(“Content-Type”, “application/json;charset=utf-8”); next(); }); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); // 后端api路由 app.use(’/api/user’, userApi); // 监听端口 httpsServer.listen(3000, function() { console.log(‘success listen at port:3000…’); });

    nginx配置:

    user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid;

    Load dynamic modules. See /usr/share/nginx/README.dynamic.

    include /usr/share/nginx/modules/*.conf;

    events { worker_connections 1024; }

    http { log_format main '$remote_addr - $remote_user [$time_local] “$request” ’

    access_log  /var/log/nginx/access.log  main;
    
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
         
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
             
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf; 
    upstream nginx_server{
        server 127.0.0.1:3000;  //服务器对应的公网ip,此处忽略
      }
    server {
        listen 443;
        server_name  www.example.com;  //我的域名,此处忽略
        # rewrite ^(.*)$  https://$host$1 permanent;  
        #root         /root/soft_index/tomcat/apache-tomcat-8.5.14/webapps/vue/;
    
        fastcgi_connect_timeout 300s;
        fastcgi_send_timeout 300s;
        fastcgi_read_timeout 300s;
    
        access_log  /usr/share/nginx/html/survey_access.log;
        error_log  /usr/share/nginx/html/survey_error.log;
    
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
    
         location /api {
         proxy_pass https://nginx_server/api;
         }
         location /{
         add_header 'Access-Control-Allow-Origin' '*';
         add_header 'Access-Control-Allow-Credentials' 'true';
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        }
    
        error_page 404 /404.html;
            location = /40x.html {
        }
    
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    

    Settings for a TLS enabled server.

    server {
        listen 443;
        server_name localhost;
        ssl on;
        root   /usr/share/nginx/html;
    
        ssl_certificate "cert/214123970110593.pem";
        ssl_certificate_key "cert/214123970110593.key";
        ssl_session_timeout  5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
    
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
    
        location / {
              root   /usr/share/nginx/html;
        }
    
        error_page 404 /404.html;
            location = /40x.html {
        }
    
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    

    } 在此之前,使用http协议,所有页面以及api都正常。 用了https协议之后,如果将我的域名上面的端口改为80端口,则页面可以使用https的协议访问,但是接口都是报404,但是,我想https的默认端口不是443吗,改成443,连页面都访问不了。希望大佬给们指出哪里错了。



沪ICP备19023445号-2号
友情链接