鍍金池/ 問答/Linux/ nginx配置問題

nginx配置問題

由于生產(chǎn)環(huán)境限制,只能對(duì)外開放一個(gè)端口9988,其他人寫的項(xiàng)目也要用這個(gè)端口,所以只能用不同的請(qǐng)求路徑來區(qū)分不同的項(xiàng)目。

我的項(xiàng)目是前后分離的。頁面打包的內(nèi)容放到服務(wù)器目錄是:/usr/local/m2m/nbMsg/browser/dist,java后臺(tái)的端口是:8989,且后臺(tái)接口統(tǒng)一前綴/api/v1。

我想配置成訪問http://[host]:9988/nbmsg,能夠默認(rèn)返回/usr/local/m2m/nbMsg/browser/dist目錄下的html靜態(tài)頁面文件,然后頁面上的ajax請(qǐng)求都是/api/v1開頭的,需要代理到8989端口。

下面是我的配置。一個(gè)server是原來已有的9988端口的虛擬主機(jī),另一個(gè)8999端口的虛擬主機(jī)是我加上去的。我應(yīng)該如何修改配置?讓http://[host]:9988/nbmsg的請(qǐng)求都轉(zhuǎn)發(fā)到8999端口的虛擬主機(jī)上?

server {
    listen 9988;
    root /server/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.php index.nginx-debian.html;

    location / {
        index index.html index.htm index.php;
    #    try_files $uri $uri/ =404;
    }

    location /business/ {
        #    root /server/html;
        try_files $uri $uri/ /business/public/index.php?$query_string;
    }

####################################################################
# request entrance
####################################################################
        location /nbmsg {
                proxy_pass http://127.0.0.1:8999;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real_IP $remote_addr;
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;

        location = /50x.html {
                root /server/html;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        add_header Access-Control-Allow-Origin *;
          add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
          add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
    }
}

####################################################################
# my virtural host 
####################################################################
server{
    listen 8999;     
    index index.html; 
    location / {
        alias /usr/local/m2m/nbMsg/browser/dist;
    }                                      
    location /api/v1 {            
        proxy_pass http://127.0.0.1:8989;
        proxy_pass_header Server;
        proxy_connect_timeout 600;
        proxy_send_timeout 600;
        proxy_read_timeout 600;                
        proxy_set_header Host $http_host;
        proxy_set_header X-Real_IP $remote_addr;                
        client_max_body_size 300m;
        client_body_buffer_size 256k;
        proxy_buffer_size 128k;
        proxy_buffers 8 32k;
        proxy_busy_buffers_size 128k;
        proxy_temp_file_write_size 128k;
    }
}

回答
編輯回答
尐潴豬

"由于生產(chǎn)環(huán)境限制,只能對(duì)外開放一個(gè)端口9988,其他人寫的項(xiàng)目也要用這個(gè)端口,所以只能用不同的請(qǐng)求路徑來區(qū)分不同的項(xiàng)目。" 也就是說你們通過路徑(location)來區(qū)分的應(yīng)用,并不是域名(server_name)因此不存在后續(xù)的所謂“虛擬主機(jī)”的概念。而且nginx并不存在一個(gè)虛擬主機(jī)的概念(吐槽:部分博客、技術(shù)文章里面概念亂入),參見:Server Block Examples

從你的上下文分析,你的需求:

  1. url "http://[host]:9988/nbmsg" 返回 "/usr/local/m2m/nbMsg/browser/dist" 中的內(nèi)容
  2. url "http://[host]:9988/nbmsg/api/v1" 由后端服務(wù)“ 8989”處理

順便說下,你這段含糊不清

我想配置成訪問http://[host]:9988/nbmsg,能夠默認(rèn)返回/usr/local/m2m/nbMsg/browser/dist目錄下的html靜態(tài)頁面文件,然后頁面上的ajax請(qǐng)求都是/api/v1開頭的,需要代理到8989端口。

很容易讓人理解為你的ajax完成的url是http://[host]:9988/api/v1,但從你的配置中看又不是, 同時(shí)如果你使用了html base標(biāo)簽等,但代碼中使用/api/v1類似相對(duì)路徑的話又受base標(biāo)簽影響,也就是/api/v1這個(gè)路徑的實(shí)際url僅從配置分析得出的是http://[host]:9988/nbmsg/api/v1

基于以上分析,調(diào)整后的配置如下:

server {
    listen 9988;
    root /server/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.php index.nginx-debian.html;

    location / {
        index index.html index.htm index.php;
    #    try_files $uri $uri/ =404;
    }

    location /business/ {
        #    root /server/html;
        try_files $uri $uri/ /business/public/index.php?$query_string;
    }

    ####################################################################
    # request entrance
    ####################################################################

    location /nbmsg {
        alias /usr/local/m2m/nbMsg/browser/dist;
    }
    location /nbmsg/api/v1 {
        proxy_pass http://127.0.0.1:8989;
        proxy_connect_timeout 600;
        proxy_send_timeout 600;
        proxy_read_timeout 600;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real_IP $remote_addr;
        client_max_body_size 300m;
        client_body_buffer_size 256k;
        proxy_buffer_size 128k;
        proxy_buffers 8 32k;
        proxy_busy_buffers_size 128k;
        proxy_temp_file_write_size 128k;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /server/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
        add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
    }
}
2017年1月16日 22:49
編輯回答
拼未來

給你個(gè)示例

所有 /qt /ht 的走api,其他的走 靜態(tài)頁面。
可以將 qt 那個(gè)擴(kuò)展一下~ 多復(fù)制幾份就好了~~

需要注意的點(diǎn)在靜態(tài)文件那里。 比如多個(gè)項(xiàng)目 /a, /b, /c 記得里面的 其他資源也要區(qū)分~

clipboard.png

2017年7月28日 09:49