Can Nginx represent the directory of another website?

there is no operation and maintenance in our company for the time being. I have been helping to build and configure nginx. I received a demand today. I think I can"t figure it out after reading it on the Internet, right? I would like to ask you:

our company has two servers, one a.company.com, one b.company.com
a.company.com/ is the static resource for a project
a.company.com/api is the dynamic interface for a project
b.company.com/ is the static resource for project b (internal network project, external network cannot be accessed, but want to demonstrate to customers, so there is this problem)

now you want to reverse proxy b.company.com/admin with a.company.com/admin, which is only the admin directory of the proxy b.company.com, and will not affect the functionality of the original a.company.com/ and a.company.com/api. I"ve tried several configurations and feel that I just can"t do this as a proxy for another server"s single directory. Here is my complete nginx.conf:

worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include                        mime.types;
    sendfile                       on;
    default_type                   application/octet-stream;
    keepalive_timeout              65;
    client_max_body_size           10m;
    fastcgi_intercept_errors       on;
    proxy_headers_hash_max_size    51200;
    proxy_headers_hash_bucket_size 6400;
    server {
        root        /usr/share/nginx/html;
        index       index.html;
        listen      80;
        server_name a.company.com;
        location / {
            index       index.html;
            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";
        }
        location ^~ /api/ {
            proxy_pass                 http://localhost:8888;
            proxy_buffers              4 512k;
            proxy_redirect             off;
            proxy_set_header           Host $host;
            proxy_set_header           Upgrade $http_upgrade;
            proxy_set_header           Connection "upgrade";
            proxy_set_header           X-real-ip $remote_addr;
            proxy_set_header           X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_buffer_size          512k;
            proxy_http_version         1.1;
            proxy_read_timeout         600s;
            proxy_send_timeout         600s;
            proxy_connect_timeout      600s;
            proxy_busy_buffers_size    512k;
            proxy_temp_file_write_size 512k;
        }
        location ^~ /admin/ {
            proxy_pass                 http://b.company.com/admin;
            proxy_set_header           Host $host;
            proxy_set_header           X-real-ip $remote_addr;
            proxy_set_header           X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header           REMOTE-HOST $remote_addr;
        }
    }
}

I would like to ask whether this is feasible? What should I do if it"s feasible?

May.09,2022

Please refer to the following code to configure multiple websites:

server
{
    listen 80 default_server;
    listen 443 ssl http2;
   
   server_name books.edurt.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /data/QingBook/;
   
   location / 
    {
        proxy_pass http://localhost:8181;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST $remote_addr;
        -sharp
        -sharpproxy_cache cache_one;
        -sharpproxy_cache_key $host$request_uri$is_args$args;
        -sharpproxy_cache_valid 200 304 301 302 1h;
        -sharp
        -sharpproxy_connect_timeout 30s;
        -sharpproxy_read_timeout 86400s;
        -sharpproxy_send_timeout 30s;
        -sharpproxy_http_version 1.1;
        -sharpproxy_set_header Upgrade $http_upgrade;
        -sharpproxy_set_header Connection "upgrade";
    }
   
   location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$
    {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_pass http://localhost:8181;
    }
    -sharpPROXY-END
    include enable-php-54.conf;
  
   -sharpSSL
    location ~ \.well-known{
        allow all;
    }
  
  -sharpPHP-INFO-END
    -sharpREWRITE-START URL,
    -sharpREWRITE-END
    -sharp
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    access_log  off;
}
server
{
    listen 80 default_server;
    listen 443 ssl http2;
   
   server_name books.edurt.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /data/QingBook/;
   
   location / 
    {
        proxy_pass http://localhost:8181;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST $remote_addr;
        -sharp
        -sharpproxy_cache cache_one;
        -sharpproxy_cache_key $host$request_uri$is_args$args;
        -sharpproxy_cache_valid 200 304 301 302 1h;
        -sharp
        -sharpproxy_connect_timeout 30s;
        -sharpproxy_read_timeout 86400s;
        -sharpproxy_send_timeout 30s;
        -sharpproxy_http_version 1.1;
        -sharpproxy_set_header Upgrade $http_upgrade;
        -sharpproxy_set_header Connection "upgrade";
    }
   
   location ~ .*\.(php|jsp|cgi|asp|aspx|flv|swf|xml)?$
    {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_pass http://localhost:8181;
    }
    -sharpPROXY-END
    include enable-php-54.conf;
  
   -sharpSSL
    location ~ \.well-known{
        allow all;
    }
  
  -sharpPHP-INFO-END
    -sharpREWRITE-START URL,
    -sharpREWRITE-END
    -sharp
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    access_log  off;
}

in fact, the principle is very simple: configure multiple servername and jump to different directories. If you are on different servers, you need to jump to different server address ports


if feasible, An and B servers are required on the same local area network

1, a.company.com , perform path matching. When admin is found in the path, it is directly proxied to the B server of the same local area network.

2,

location ^~ /admin {
     proxy_pass  http://  B  IP;
}
Menu