• 0
  • 新人请关照

nginx代理node接口无法调用

   server {
        listen       8086;
        server_name  39.96.90.200;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
        location /ceshi/ {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/  /ceshi/index.html;  #测试
          
        }

        location /api/ {
        proxy_set_header X-Real-IP $remote_addr;   # :8089
        proxy_pass http://localhost:3000/api/; #测试
        proxy_redirect off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-proxy true;
        }
    
    上面为nginx 反向代理的代码  外网可以访问到页面但是请求node 基于express写的数据的话就报错
    ![图片描述][1]

阅读 507
评论
    1 个回答
    • 336

    给你个标准例子:

    server {
      listen 80;
      server_name test.ml 192.168.10.1;
      access_log /data/wwwlogs/test_nginx.log combined;
      index index.html index.htm index.php;
      root /data/wwwroot/test.ml;
    
      location /api {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:7001/api; # nodejs启动的服务
      }
    
      location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
        access_log off;
      }
      location ~ .*\.(js|css)?$ {
        access_log off;
      }
      location ~ /\.ht {
        deny all;
      }
      location ~ /\.(svn|git|record) {
        return 404;
      }
    }
      相似问题
      推荐文章