鍍金池/ 問答/Linux  HTML/ 請教一個nginx配置反向代理的問題

請教一個nginx配置反向代理的問題

本地跨域配置實用的是proxy,本地測試跨域沒有問題,數(shù)據(jù)能正常接受,后臺能console

  "/api": {
    "target": "http://132.232.22.140:8889/api",
    "changeOrigin": true,
    "pathRewrite": { "^/api" : "" }
  }

部署到服務(wù)器后,nginx配置

server {
        listen 8000;
        # server_name www.alatu..xyz;

        root /home/myftp/dist;
        index index.html index.htm;

        location / {
                try_files $uri $uri/ /index.html;
        }
        location ^~ /assets/ {
                gzip_static on;
                expires max;
                add_header Cache-Control public;
        }
location /api {
    rewrite  ^.+api/?(.*)$ /$1 break;
    include  uwsgi_params;
       proxy_pass   http://localhost:8889;
       }
        error_page 500 502 503 504 /500.html;
        client_max_body_size 20M;
        keepalive_timeout 10;
}

線上測試收不到數(shù)據(jù),后臺也沒有console啊,有沒有前輩幫忙看看,不甚感激!

回答
編輯回答
刮刮樂

看nginx日志,包括access_log和error_log

2017年3月22日 11:13
編輯回答
殘淚

結(jié)案了,原來最大的問題是我的后臺沒寫對,url寫錯了

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  132.232.22.140:8000;
        root         /usr/share/nginx/html;
#       root          /home/myftp/dist

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location ^~ /api/ {
            proxy_pass http://127.0.0.1:8889;
        }
        location /{
                proxy_pass   http://127.0.0.1:8000;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    

8000前端端口,8889后臺端口,這樣寫就跨域成功了

2018年9月21日 01:05