Php 为同一域上的多个站点提供服务

Php 为同一域上的多个站点提供服务,php,nginx,Php,Nginx,我在部署方面是新手,基本上这是我第一次接触它。应用程序结构简介: 我有三个部分: api.app.dev/——用流明书写 app.dev/backend/-基本PHP中间件,用于保存API令牌和用户数据 app.dev/-它是前端(JS) 我正在使用nginx。 我花了那么多时间试着把它设置好。问题是在app.dev/我有/template文件夹,其中存储了PHP模板。 在app.dev/backend/中,我只有一个页面可以处理请求 在涉及API之前。配置应该是什么样子 我成功地配置了AP

我在部署方面是新手,基本上这是我第一次接触它。应用程序结构简介:

我有三个部分:

  • api.app.dev/
    ——用流明书写
  • app.dev/backend/
    -基本PHP中间件,用于保存API令牌和用户数据
  • app.dev/
    -它是前端(JS)
我正在使用
nginx
。 我花了那么多时间试着把它设置好。问题是在
app.dev/
我有
/template
文件夹,其中存储了PHP模板。 在app.dev/backend/中,我只有一个页面可以处理请求 在涉及API之前。配置应该是什么样子

我成功地配置了API。前端目前可以工作,但我无法测试它


但无法使后端部件正常工作。有以下当前配置:

app.dev/backend 如何访问后端部件? -它是通过前端访问的。AJAX请求被发送到下面的URL。 当我尝试访问时:
app.dev/backend/?action=1123
我发现
404页未找到

在localhost上,一切都像魅力一样工作。我使用PHP内部服务器开发,这是一个很大的错误

好吧,我通过大量的谷歌搜索和尝试解决了我的问题。以下是虚拟主机:

api.app.dev app.dev/(&&app.dev/backend/)
“无法使后端部件正常工作”。到底是什么不起作用?怎么了?你没有告诉我们问题出在哪里。@ADyson我很抱歉告诉你,小姐,但是你可以写下这篇文章,不投反对票……首先,你为什么确定我投了反对票?可能是任何人。碰巧,这次是我,是的。我可以发表评论,但人们对否决票的反应更积极……无论如何,一旦有足够的信息,我会很乐意收回它。否决票是因为缺乏明确性,根据SO的说法,这是一个合理的理由。无论如何,一个URL上的404并不是足够的证据。所有访问app.dev/backend的访客是否都会导致404?还是就那个?你检查过所有的东西都正确部署了吗?您的代码在另一个环境中正常工作吗?@ADyson:)我知道,我知道:)但请记住权限也会降级。信息已更新。根据您的问题,您有两个域
api.app.dev
app.dev
——后一个域同时支持
/
位置和
/backend
位置。
/
/backend
位置的所有配置应显示在同一
服务器
块中,并带有
服务器名称app.dev指令。
server {

    # Port that the web server will listen on.
    listen         80;

    # Host that will serve this project.
    server_name     hr.dev/backend;

    # Useful logs for debug.


    access_log      /var/log/nginx/access-hr-backend.log main;
    error_log       /var/log/nginx/error-hr-backend.log;
    rewrite_log     on;

    # The location of our projects public directory.
    root            /var/www/hr_app/git_repository/backend;

    index           page.php;


    location / {
    add_header Access-Control-Allow-Origin "http://hr.dev";
    add_header Access-Control-Allow-Credentials true;

        # URLs to attempt, including pretty ones.
        try_files  $uri/ /page.php?$query_string;

    }

    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
        rewrite     ^/(.+)/$ /$1 permanent;
    }

    # PHP FPM configuration.
    location ~* \.php$ {

            add_header Access-Control-Allow-Origin "http://hr.dev";
        add_header Access-Control-Allow-Credentials true;
            fastcgi_pass                    unix:/var/run/php/php7.1-fpm.sock;
            fastcgi_index                   page.php;
            fastcgi_split_path_info         ^(.+\.php)(.*)$;
            include                         /etc/nginx/fastcgi_params;
            fastcgi_param                   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # We don't need .ht files with nginx.
    location ~ /\.ht {
            deny all;
    }

    location ~ \.css {
        add_header  Content-Type    text/css;
        add_header Access-Control-Allow-Origin *;

    }

    location ~ \.js {
        add_header  Content-Type    application/x-javascript;
        add_header Access-Control-Allow-Origin *;

    }


    # Set header expirations on per-project basis
    location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
            add_header Access-Control-Allow-Origin *;

            expires 365d;

    }



}
server {

    # Port that the web server will listen on.
    listen          80;

    # Host that will serve this project.
    server_name     api.app.dev;

    # Useful logs for debug.
    access_log      /var/log/nginx/access-hr-api.log main;
    error_log       /var/log/nginx/error-hr-api.log;
    rewrite_log     on;

    # The location of our projects public directory.
    root            /var/www/app/api/public;

    # Point index to the Laravel front controller.
    index           index.php;



    location / {

        # URLs to attempt, including pretty ones.
        add_header Access-Control-Allow-Origin *;
        try_files   $uri $uri/ /index.php?$query_string;

    }

    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
        rewrite     ^/(.+)/$ /$1 permanent;
    }

    # PHP FPM configuration.
    location ~* \.php$ {

        add_header Access-Control-Allow-Origin *;

        fastcgi_pass                    unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_index                   index.php;
        fastcgi_split_path_info         ^(.+\.php)(.*)$;
        include                         /etc/nginx/fastcgi_params;
        fastcgi_param                   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # We don't need .ht files with nginx.
    location ~ /\.ht {
            deny all;
    }

    # Set header expirations on per-project basis
    location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
            expires 365d;

    }

}
server {

    # Port that the web server will listen on.
    listen         80;

    # Host that will serve this project.
    server_name     app.dev;

    # Useful logs for debug.

    root /var/www/app;
    index index.html page.php;

    access_log      /var/log/nginx/access-hr.log main;
    error_log       /var/log/nginx/error-hr.log;
    rewrite_log     on;


    location /backend {
          add_header Test "location /backend ";
          add_header Access-Control-Allow-Origin "http://hr.dev";
          add_header Access-Control-Allow-Credentials true;
          alias /var/www/app/backend;

          # URLs to attempt, including pretty ones.
          try_files  $uri/ /page.php?$query_string;
    }


    location / {
         add_header Test "location / in frontent";
         add_header Test "location / in frontend vhost";
         add_header Access-Control-Allow-Origin "app.dev";
         add_header Access-Control-Allow-Credentials true;
         root /var/www/app/frontend;
         index index.html;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection 'upgrade';
         proxy_set_header Host $host;
         proxy_cache_bypass $http_upgrade;
         index index.html;

    }


    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
        rewrite     ^/(.+)/$ /$1 permanent;
    }


     location  /frontend/template {
         alias /var/www/app/frontend;
     }

    # PHP FPM configuration.
    location ~* \.php$ {
          add_header Test "location php in backend ";
          add_header Access-Control-Allow-Origin "http://app.dev";
          add_header Access-Control-Allow-Credentials true;
          fastcgi_pass                     unix:/var/run/php/php7.1-fpm.sock;
          fastcgi_index                   index.php;
          fastcgi_split_path_info    ^(.+\.php)(.*)$;
          include                            /etc/nginx/fastcgi_params;
          fastcgi_param                  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # We don't need .ht files with nginx.
    location ~ /\.ht {
        add_header Test "location ht in backend ";
        deny all;
    }

    location ~ \.css {
        add_header Test "location css in hr.dev";
        add_header  Content-Type    text/css;
        add_header Access-Control-Allow-Origin *;
        root /var/www/app/frontend;

    }

    location ~ securimage.js {
        add_header  Content-Type    application/x-javascript;
        root /var/www/app;

    }
    location ~ \.js {
        add_header Test "location js in hr.dev";
        add_header  Content-Type    application/x-javascript;
        add_header Access-Control-Allow-Origin *;
        root /var/www/app/frontend;

    }


    # Set header expirations on per-project basis
    location ~* \.(?:ico|jpe?g|JPG|png|svg|woff)$ {
            add_header Test "location ico,js,jpeg... in backend";
            add_header Access-Control-Allow-Origin *;
            expires 365d;

    }

}