账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    nginx在同一个域名下部署多个项目
    76
    0

    nginx配置如下
    nginx.conf

    user www-data;
    worker_processes auto;
    pid /run/nginx.pid;
    
    events {
            worker_connections 768;
            # multi_accept on;
    }
    
    http {
            sendfile on;
            keepalive_timeout 65;
            types_hash_max_size 2048;
    
            include /etc/nginx/mime.types;
            default_type application/octet-stream;
    
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
            ssl_prefer_server_ciphers on;
    
            access_log /var/log/nginx/access.log;
            error_log /var/log/nginx/error.log;
    
            gzip on;
            gzip_disable "msie6";
    
            access_log /var/log/nginx/access.log;
            error_log /var/log/nginx/error.log;
            
            gzip on;
            gzip_disable "msie6";
    
            include /etc/nginx/conf.d/*.conf;
            include /etc/nginx/sites-enabled/*;
    }
    

    sites-enabled > default

    
    server {
            listen 80 default_server;
            listen [::]:80 default_server;
            
            root /var/html
            # Add index.php to the list if you are using PHP
            index index.html index.htm index.nginx-debian.html;
    
            server_name _;
            
            location /admin/ {
                alias /home/cms-admin/dist;
                index index.html index.htm;
            }
            
            location / {
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    proxy_pass http://127.0.0.1:3000;
            }
            
            location ~ .*\.(js|css|png|jpg|gif|swf|ico|pdf|mov|mp3|wav|json|woff2)$ {
                proxy_pass http://127.0.0.1:3000;
            }
    
            location /api/ {
                proxy_pass  http://127.0.0.1:3002/api/;
            }
    
    }
    

    3000端口跑了一个ssr项目,现在的配置可以正常访问,例如:xxx/index,可以直接拿到ssr项目index页面

    location ~ .*\.,这个规则是配合ssr项目的,去拿ssr项目的静态文件

    /home/cms-admin/dist这个目录下是另一个项目的打包文件,我需要通过访问xxx/admin拿到dist目录下的index.html文件(如果把其他的规则都删除,只保留location /admin/这个规则,可以正常拿到页面)

    现在的问题是:
    当我访问xxx/admin的时候,直接进入location /这个规则中,返回了ssr项目中admin路由页面(ssr项目不存在这个路由,所以返回了一个空的页面)

    而我希望的效果是访问xxx/admin路由,直接去拿dist目录下的index.html

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 月宫中人 普通会员 1楼

      在Nginx中部署多个项目,您需要创建多个服务器块(server blocks),每个服务器块对应一个项目,并将其添加到Nginx的httpserver配置文件中。

      以下是一个基本的示例,展示如何在同一个域名下部署两个项目:

      1. 创建一个名为project1的项目(例如project1.conf):

      ```nginx server { listen 80; server_name example.com;

      location / {
          root /var/www/project1;
          index index.html index.htm;
      }
      

      } ```

      1. 创建一个名为project2的项目(例如project2.conf):

      ```nginx server { listen 80; server_name example.com;

      location / {
          root /var/www/project2;
          index index.html index.htm;
      }
      

      } ```

      1. 使用location块将项目的HTML文件定向到对应的服务器块:

      ```nginx server { listen 80; server_name example.com;

      location / {
          root /var/www/project1;
          index index.html index.htm;
      }
      
      location / {
          root /var/www/project2;
          index index.html index.htm;
      }
      

      } ```

      在这个示例中,两个项目都有各自的HTML文件,分别在/var/www/project1/var/www/project2目录下。当访问example.com时,Nginx会根据当前域名转发到相应的项目。

      请注意,这只是一个基本的示例,实际的配置可能需要根据您的具体需求进行调整。例如,您可能需要在多个项目之间共享相同的资源(例如CSS、JavaScript等),或者需要使用不同的URL路径映射。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部