在同一域中服务多个站点 [英] Serving multiple sites on the same domain

查看:55
本文介绍了在同一域中服务多个站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在部署方面并不陌生,基本上这是我第一次与它取得联系.有关应用程序结构的简短说明:

I'm new at deploying, and basically this is the first time i get in touch with it. Short about application structure:

我分为三个部分:

  • api.app.dev/-用流明编写,
  • app.dev/backend/-基本的PHP中间件,用于保留API令牌和用户数据,
  • app.dev/-这是前端(JS).
  • api.app.dev/ - which is written in Lumen,
  • app.dev/backend/ - basic PHP middleware, used to keep API token and user data,
  • app.dev/ - which is front-end (JS).

我正在使用nginx. 我花了很多时间尝试进行设置.问题是在app.dev/,我有/template文件夹,用于存储PHP模板. 在app.dev/backend/,我只有一页要处理的页面 在介绍API之前.配置应该是什么样子?

I'm using nginx. I spent so much time trying to set it up. The problem is that at app.dev/ i have /template folder where PHP templates are stored. At app.dev/backend/ i have just one page which processing request before it comes to API. How configuration should looks like?

我已成功配置API.前端目前可以使用,但我无法对其进行测试.

I successfully configured API. Front-end works for now, but i can't test it.

但是无法使后端部分正常工作.当前配置为:

But can't get back-end part working. There is current configuration:

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;

    }



}

如何访问后端部分? -通过前端访问. AJAX请求被发送到下面的URL. 当我尝试访问时:app.dev/backend/?action=1123我得到了404 page not found. 在本地主机上,一切都像魅力.我使用PHP内部服务器进行开发,那是一个大错误!

How do back-end part is accessed? - It's accessed via front-end. AJAX request is sent to URL below. When i try to access: app.dev/backend/?action=1123 i get 404 page not found. On localhost everything works like charm. I develop with PHP internal server, and that was a BIG mistake!

推荐答案

好,我通过大量的Google搜索和尝试解决了我的问题.有虚拟主机:

Ok, i solved my problem by a lot of googling and trying. There are vhosts:

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;

    }

}

app.dev/(&& app.dev/backend/)

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;

    }

}

这篇关于在同一域中服务多个站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆