You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopware 6.4.x Nginx配置模板咨询及规则转换问题

Shopware 6.4.x Nginx 配置模板及规则转换方案

完整Nginx配置模板

直接将以下配置添加到你的Nginx server块中(根据实际部署路径调整root和域名):

server {
    listen 80;
    server_name your-shop-domain.com;
    root /var/www/shopware/public; # 指向Shopware的public目录
    index index.php index.html;

    # 静态文件缓存设置
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        try_files $uri =404;
    }

    # 媒体文件处理
    location /media {
        try_files $uri /index.php$is_args$args;
    }

    # 主题文件处理
    location /theme {
        try_files $uri /index.php$is_args$args;
    }

    # 核心路由重写
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # PHP处理
    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

        # 传递BASE环境变量(对应你要转换的Apache规则)
        fastcgi_param BASE $request_uri;
        # 如果是子目录部署,比如/shopware,直接指定:
        # fastcgi_param BASE /shopware;
    }

    # 禁止访问敏感文件
    location ~ /\.ht {
        deny all;
    }

    location ~ /(var|vendor|platform/src) {
        deny all;
    }
}

关于你提到的Apache规则转换

你说的这条Apache规则:

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

作用是提取当前请求的基础路径(比如店铺部署在子目录时的路径前缀),并将其设置为BASE环境变量,供后续的路由规则使用。

在Nginx里不需要这么复杂的正则匹配,直接通过变量就能实现:

  • 如果店铺部署在网站根目录,直接在PHP处理块中添加:fastcgi_param BASE /;
  • 如果部署在子目录(比如/my-shop),直接指定:fastcgi_param BASE /my-shop;
  • 要是需要动态适配请求路径,也可以用Nginx的变量推导:fastcgi_param BASE $uri;

这样就能完全替代Apache那条规则的作用,确保Shopware的路由正常工作。

内容的提问来源于stack exchange,提问作者Tobias Herweg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 01:12:10