Nginx 可以代理另一个网站的目录吗?

jmyz_0455
  • 72

我司暂时没有运维,一直都是我在帮忙搭建和配置 nginx ,今天接到一个需求,我上网看一圈之后觉得弄不出来的吧?想请教一下各位:

我司有两台服务器,一个 a.company.com ,一个 b.company.com
a.company.com/ 是放 a 项目的静态资源
a.company.com/api 是访问 a 项目动态接口
b.company.com/ 是放 b 项目的静态资源(内网项目,外网无法访问,但是想演示给客户看,所以有了这个问题)

现在想用 a.company.com/admin 反向代理 b.company.com/admin ,仅仅是代理 b.company.com 的 admin 目录,不会影响原来 a.company.com/ 和 a.company.com/api 的功能。我试过好几个配置,感觉就是不能这样做另一个服务器单个目录的代理。下面是我的完整 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;
        }
    }
}

我想问这是可行的吗?如果可行要怎么弄呢?

回复
阅读 3.5k
2 个回答
✓ 已被采纳

请参照以下代码配置多网站:

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;
        #缓存相关配置
        #proxy_cache cache_one;
        #proxy_cache_key $host$request_uri$is_args$args;
        #proxy_cache_valid 200 304 301 302 1h;
        #持久化连接相关配置
        #proxy_connect_timeout 30s;
        #proxy_read_timeout 86400s;
        #proxy_send_timeout 30s;
        #proxy_http_version 1.1;
        #proxy_set_header Upgrade $http_upgrade;
        #proxy_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;
    }
    #PROXY-END
    include enable-php-54.conf;
  
   #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }
  
  #PHP-INFO-END
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    #REWRITE-END
    #禁止访问的文件或目录
    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;
        #缓存相关配置
        #proxy_cache cache_one;
        #proxy_cache_key $host$request_uri$is_args$args;
        #proxy_cache_valid 200 304 301 302 1h;
        #持久化连接相关配置
        #proxy_connect_timeout 30s;
        #proxy_read_timeout 86400s;
        #proxy_send_timeout 30s;
        #proxy_http_version 1.1;
        #proxy_set_header Upgrade $http_upgrade;
        #proxy_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;
    }
    #PROXY-END
    include enable-php-54.conf;
  
   #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }
  
  #PHP-INFO-END
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    #REWRITE-END
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    access_log  off;
}

其实原理很简单就是配置多个servername,跳转到不同的目录,如果是在不同服务器上的话,那就需要跳转到不同的服务器地址端口上

可行的,需要 A、B 服务器在同一局域网

1、a.company.com,进行路径匹配,当查询到路径中有 admin 的时候,直接代理到同一局域网的 B 服务器上。

2、

location ^~ /admin {
     proxy_pass  http:// 服务器 B 的 IP和端口;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏