鍍金池/ 問答/Linux  HTML/ nginx 配置的listen監(jiān)聽端口和vue-cli啟動(dòng)的端口沖突了,怎么辦?

nginx 配置的listen監(jiān)聽端口和vue-cli啟動(dòng)的端口沖突了,怎么辦?

我通過nginx反向代理解決跨域問題,啟動(dòng)項(xiàng)目,地址為localhost:8142, 然后配置nginx的8142端口。
nginx報(bào)錯(cuò)

nginx: [emerg] bind() to 0.0.0.0:8142 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
server {
        listen       8142;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /Webapi {
            rewrite  ^.+Webapi/?(.*)$ /$1 break;
            include  uwsgi_params;
            proxy_pass   http://www.baidu.com;
       }

應(yīng)該怎么解決?

回答
編輯回答
黑與白

你的應(yīng)用是8142端口,你用nginx代理就不要用這個(gè)端口了啊,換個(gè)端口

2018年2月4日 13:38
編輯回答
老梗

改變你vue-cli的端口地址就成!

2018年8月12日 15:03
編輯回答
生性

要么修改你nginx的監(jiān)聽端口, 要么改vue-cli配置的webpack配置的端口.

表達(dá)能力比較弱, 不理解再說

這是我nginx的配置, 監(jiān)聽80即頁面默認(rèn)端口.
當(dāng)Nginx接收到api路徑的時(shí)候, 轉(zhuǎn)發(fā)給8070端口.會(huì)改寫成(就是去掉/api,然后轉(zhuǎn)發(fā))http://127.0.0.1:8070/...

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location ^~/api/ {
            rewrite ^/api/(.*) /$1 break;
            proxy_pass http://127.0.0.1:8070;
        }
       ...
      }

這個(gè)是我測試環(huán)境的反向代理配置.
我前端測試的端口號是:8081
通過設(shè)置完這個(gè)proxyTable之后, 我的請求路徑只要是定位為/api就會(huì)變成http://localhost/api/....

    proxyTable: {
      '/api': {
        target: 'http://localhost/',
        changeOrigin: true,
        pathRewrite: {
          // '^/api': '' 這里注釋掉了, 是因?yàn)槲襫ginx配置好了攔截api, 所以這里不改寫也沒問題
        }
      }
    }
    

文檔描述proxyTable

2018年4月5日 05:06