Nginx:在不同的根目录下为 magento 站点提供服务,并在其他 magento 根目录下设置子目录

Nginx:在不同的根目录下为 magento 站点提供服务,并在其他 magento 根目录下设置子目录

经过几个小时的谷歌搜索和测试,我还没有让它工作。我在 /srv/site1/htdocs 上安装了可以正常运行的 magento。我在 /srv/site2/htdocs 上安装了另一个 magento。我需要在 site1.com/site2/ 上加载 site2。很棘手吧?

到目前为止,我已经让 site1.com/site2 为 site2 magento 安装加载了一个未设置样式的 404 页面,并且此目录内的所有其他内容都会引发 404。这是我的整个 nginx 配置:

server {
    listen 80;
    server_name site1.com;
    root /srv/site1.com/htdocs;
    index index.php;

    client_max_body_size 500M;


    location ~ /site2 {           
        root /srv/site2/htdocs;
        add_header Access-Control-Allow-Origin "*";
        index index.php index.html;
        try_files $uri $uri/ /index.php?$args;
        expires 30d;

        location ~* \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_pass    unix:/var/run/php-fpm/www.sock;
            fastcgi_index   index.php;
            include fastcgi_params;
            fastcgi_read_timeout 20000000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include mime.types;
        }

        location ~ /site2/.+\.php$ {
            root /srv/site2/htdocs;
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_pass    unix:/var/run/php-fpm/www.sock;
            fastcgi_index   index.php;
            include fastcgi_params;
            fastcgi_read_timeout 20000000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include mime.types;
        }


    }


    location / {
        add_header Access-Control-Allow-Origin "*";
        index index.php; ## Allow a static html file to be shown first
            try_files $uri $uri/ /index.php?$args;
        expires 30d;
    }


    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types
        text/plain
        text/css
        text/js
        text/xml
        text/javascript
        application/javascript
        application/x-javascript
        application/json
        application/xml
        application/xml+rss;


## These locations would be hidden by .htaccess normally
#location ^~ /app/                { deny all; }
    location ^~ /includes/           { deny all; }
    location ^~ /lib/                { deny all; }
    location ^~ /media/downloadable/ { deny all; }
    location ^~ /pkginfo/            { deny all; }
    location ^~ /report/config.xml   { deny all; }
    location ^~ /var/                { deny all; }

    location /var/export/ { ## Allow admins only to view export folder
        auth_basic           "Restricted"; ## Message shown in login window
            auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
            autoindex            on;
    }

    location ~ /\.htaccess { ## Disable .htaccess and other hidden files
        return 404;
    }

    location ~ \.php$ {
        if ($request_uri ~ /site2/.*$) {
            root /srv/site2/htdocs;
        }

        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass    unix:/var/run/php-fpm/www.sock;
        fastcgi_index   index.php;
        include fastcgi_params;
        fastcgi_read_timeout 20000000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include mime.types;
    }


    location ~* \.(?:manifest|appcache|html?|xml|json)$ {
        expires -1;
    }

# Feed
    location ~* \.(?:rss|atom)$ {
        expires 1h;
        add_header Cache-Control "public";
    }

# Media: images, icons, video, audio, HTC
    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
    }

# CSS and Javascript
    location ~* \.(?:css|js)$ {
        expires 1y;
        access_log off;
        add_header Cache-Control "public";
    }
}

肯定有一些冗余代码。我快要束手无策了。有什么想法吗?我还需要为另一个 magento 网站 site1.com/site3 执行此操作。确实很有趣。

相关内容