不能使用多个项目

Posted

技术标签:

【中文标题】不能使用多个项目【英文标题】:Can not use multiple projects 【发布时间】:2019-04-25 21:17:23 【问题描述】:

我正在尝试将我的 nginx 配置为按路径使用多个项目。

所以我有一个基于javascript(VueJs 项目)的客户端,它向 api 发送请求。 api 的根源以/api(Laravel 项目)开头。 我也有基于 Laravel 的管理面板。管理面板的 url 将以 /admin 开头。

这是我的nginx 配置文件

server 
    server_name cabinet.mydomain.org; 

   # auth_basic "Restricted Content";
   # auth_basic_user_file /etc/nginx/.htpasswd;

   # access_log /var/www/cabinet/access.log;
   # error_log /var/www/cabinet/error.log;

    root /var/www/cabinet/api/html/public;

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

    #location / 
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
    #   try_files $uri $uri/ =404;
    #

    location /api 
          access_log /var/log/cabinet.api.acc.log;
          error_log /var/www/cabinet.api.error.log debug;
          try_files $uri $uri /index.php$args;
        

        location /admin 
          access_log /var/log/cabinet.admin.acc.log;
          error_log /var/www/cabinet.admin.error.log debug;
      root /var/www/cabinet-admin/public;
          try_files $uri $uri /index.php$args;
        



        location / 
          try_files $uri $uri/ /index.html;
          root /var/www/cabinet/client/dist;
        

    location = / 
          return 301 $scheme://$server_name/login/;
        


#        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ 
#       return 500; 
           # Some basic cache-control for static files to be sent to the browser
#           expires max;
#           add_header Pragma public;
#           add_header Cache-Control "public, must-revalidate, proxy-revalidate";
#       

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #

    location ~ \.php$ 
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht 
    #   deny all;
    #

 # managed by Certbot

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/cabinet.mydomain.org-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/cabinet.mydomain.org-0001/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot



server 
    if ($host = cabinet.mydomain.org) 
        return 301 https://$host$request_uri;
     # managed by Certbot


    #if ($host = cabinet.mydomain.com) 
    #    return 301 https://cabinet.mydomain.org$request_uri;
    # # managed by Certbot

    #if ($host = cabinet.mydomain.org) 
    #    return 301 https://$host$request_uri;
    # # managed by Certbot

    server_name cabinet.mydomain.org cabinet.mydomain.com;
    listen 80;
    return 301 https://cabinet.mydomain.org$request_uri;
    return 404; # managed by Certbot



因此,当尝试访问 /admin 时,服务器会重定向到 /login

请帮忙解决这个问题

【问题讨论】:

您是否尝试过 api 和管理位置块:try_files $uri $uri/ /index.php$args?注意第二个 $uri 参数末尾的斜线。否则看起来还不错.. 我需要设置哪个块? /api 和 /admin,但正如我所检查的:如果位置匹配,如果找不到文件或目录,它将不会使用其他位置块。所以 /admin 应该被抓住或给出 404,而不是退回到 = / 【参考方案1】:

试试这个。

server 
    server_name cabinet.mydomain.org; 

   # auth_basic "Restricted Content";
   # auth_basic_user_file /etc/nginx/.htpasswd;

   # access_log /var/www/cabinet/access.log;
   # error_log /var/www/cabinet/error.log;

    root /var/www;

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

    #location / 
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
    #   try_files $uri $uri/ =404;
    #

    location /api 
          access_log /var/log/cabinet.api.acc.log;
          error_log /var/www/cabinet.api.error.log debug;
          root PATH_TO_YOUR_PROJECT;
          try_files $uri $uri /PATH_TO_PROJECT/index.php$args;
        

        location /admin 
          access_log /var/log/cabinet.admin.acc.log;
          error_log /var/www/cabinet.admin.error.log debug;
          root /var/www/cabinet-admin/public;
          try_files $uri $uri /cabinet-admin/public/index.php$args;
        

请关注与

相关的文档

$try_files

root

【讨论】:

无法重启 nginx 。第 23 行有错误 @KhazarAmjer 确保您已将 PATH_TO_PROJECT 更改为真实路径

以上是关于不能使用多个项目的主要内容,如果未能解决你的问题,请参考以下文章

typescript 解决方案“错误:gulp-typescript:项目不能同时用于两个编译*。使用createP创建多个项目

SpringSpring 多个项目的 cookie 互相覆盖 导致不能访问

怎样设置多个tomcat使用同一个端口

如何访问 *ngFor 中的“索引”并对其进行编辑(不能删除列表中的多个项目)

Spring项目中Properties不能加载多个的问题

怎么在IDEA的一个窗口中打开多个项目