Nginx configuration issu

due to the limitation of production environment, only one port can be opened 9988 . Projects written by others also use this port, so different request paths can only be used to distinguish different projects.

my project is separate from front to back. The content packaged on the page is placed in the server directory: / usr/local/m2m/nbMsg/browser/dist , the port of the java backend is 8989 , and the background interface is uniformly prefixed with / api/v1 .

I want to configure to access http://[host]:9988/nbmsg, which can return the html static page file under the / usr/local/m2m/nbMsg/browser/dist directory by default. Then all ajax requests on the page start with / api/v1 and need to be proxied to port 8989 .

here is my configuration. One server is the virtual host of the existing 9988 port, and the other virtual host of the 8999 port is added by me. How should I modify the configuration? Let the requests of http://[host]:9988/nbmsg be forwarded to the virtual host on the 8999 port?

server {
    listen 9988;
    root /server/html;

    -sharp 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;
    -sharp    try_files $uri $uri/ =404;
    }

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

-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp
-sharp request entrance
-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp
        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";
    }
}

-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp
-sharp my virtural host 
-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp
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;
    }
}

Mar.22,2021

"due to the limitation of the production environment, only one port 9988 can be opened to the public, and projects written by others also use this port, so different request paths can only be used to distinguish different projects." In other words, the application you distinguish by path (location) is not a domain name (server_name), so there is no subsequent concept of "virtual host". And nginx does not have the concept of a virtual host (complain: some blogs and technical articles have misleading concepts). See Server Block Examples

.

from your context, your requirements:

  1. url "http://[host]:9988/nbmsg" returns the contents of" / usr/local/m2m/nbMsg/browser/dist "
  2. url "http://[host]:9988/nbmsg/api/v1" is handled by backend service" 8989 "

by the way, your paragraph is ambiguous

I want to configure to access http://[host]:9988/nbmsg, which can return the html static page file in the / usr/local/m2m/nbMsg/browser/dist directory by default, and then all the ajax requests on the page start with / api/v1 and need to be proxied to port 8989.

it is easy for people to understand that the url completed by your ajax is http://[host]:9988/api/v1, but not from your configuration. At the same time, if you use the html base tag, etc., but if you use a relative path similar to / api/v1 in the code, it is also affected by the base tag, that is, the actual url of the path / api/v1 is only http://[host]:9988/nbmsg/api/v1

from the configuration analysis.

based on the above analysis, the adjusted configuration is as follows:

server {
    listen 9988;
    root /server/html;

    -sharp 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;
    -sharp    try_files $uri $uri/ =404;
    }

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

    -sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp
    -sharp request entrance
    -sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp

    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";
    }
}

I'll give you an example

all / qt / ht go to api, and others go to static pages.
you can extend the qt a little bit ~ just make a few more copies ~

the point to pay attention to is in the static file. For example, multiple projects / a, / b, / c remember that other resources in it should also be distinguished.

clipboard.png

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3736f-2b8c1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3736f-2b8c1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?