PrestaShop 的 Nginx 伪静态(URL 重写)规则,按你的域名、目录、PHP-FPM 和 HTTPS 生成完整 server 配置或片段,附 Apache .htaccess 写法,并注明规则来源。
已对照官方文档 来源: devdocs.prestashop-project.org · Configure Nginx
server { listen 80; listen [::]:80; server_name example.com; location ^~ /.well-known/acme-challenge/ { root /var/www/_letsencrypt; } location / { return 301 https://$host$request_uri; } } server { listen 443 ssl; listen [::]:443 ssl; http2 on; server_name example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # Mozilla Intermediate(Mozilla Server Side TLS 6.0) ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305; ssl_prefer_server_ciphers off; ssl_session_timeout 1d; ssl_session_cache shared:SSL:10m; ssl_session_tickets off; # 不开 OCSP stapling:Let's Encrypt 已于 2025 年停止 OCSP 服务。 root /var/www/example.com; index index.php index.html; server_tokens off; client_max_body_size 16m; # 安全头(只写在 server 层;location 里一旦写 add_header 就要整组重复) add_header X-Content-Type-Options "nosniff" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always; # CSP 需要按站点调整,确认不会挡掉自己的脚本后再启用: # add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'self'" always; add_header Strict-Transport-Security "max-age=63072000" always; gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_min_length 256; gzip_types text/plain text/css text/xml application/json application/javascript application/xml application/rss+xml application/atom+xml image/svg+xml; error_page 404 /index.php?controller=404; # 商品图片 rewrite ^/(\d)(-[\w-]+)?/.+\.(jpe?g|png|webp|avif)$ /img/p/$1/$1$2.$3 last; rewrite ^/(\d)(\d)(-[\w-]+)?/.+\.(jpe?g|png|webp|avif)$ /img/p/$1/$2/$1$2$3.$4 last; rewrite ^/(\d)(\d)(\d)(-[\w-]+)?/.+\.(jpe?g|png|webp|avif)$ /img/p/$1/$2/$3/$1$2$3$4.$5 last; rewrite ^/(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.(jpe?g|png|webp|avif)$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.$6 last; rewrite ^/(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.(jpe?g|png|webp|avif)$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.$7 last; rewrite ^/(\d)(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.(jpe?g|png|webp|avif)$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.$8 last; rewrite ^/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.(jpe?g|png|webp|avif)$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.$9 last; rewrite ^/(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(-[\w-]+)?/.+\.(?<ext>jpe?g|png|webp|avif)$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.$ext last; rewrite ^/c/([\w.-]+)/.+\.(jpe?g|png|webp|avif)$ /img/c/$1.$2 last; rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last; location / { try_files $uri $uri/ /index.php$is_args$args; } # 把 admin-dev 改成你的后台目录名 location /admin-dev/ { try_files $uri $uri/ /admin-dev/index.php$is_args$args; } location ~ /\.(?!well-known) { deny all; } location ~ ^/(app|bin|cache|classes|config|controllers|docs|localization|override|src|tests|tools|translations|var|vendor)/ { deny all; } location ~ ^/modules/.*/vendor/ { deny all; } location ~ \.(log|tpl|twig|sass|yml)$ { deny all; } location /img { location ~ \.php$ { deny all; } } location /upload { location ~ \.php$ { deny all; } } location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; try_files $fastcgi_script_name =404; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_index index.php; fastcgi_keep_conn on; fastcgi_read_timeout 30s; fastcgi_send_timeout 30s; fastcgi_pass unix:/run/php/php8.3-fpm.sock; } }
# PrestaShop 后台「商店参数 → 流量与 SEO」开启友好 URL 后会自动生成 .htaccess
生成的 Nginx 配置都通过了本站 配置体检;上线前请在服务器上 nginx -t。只有首页能开、内页 404 见 404 & try_files.
放进这个站点的 server {} 块里(通常是 /etc/nginx/conf.d/域名.conf 或宝塔面板「网站 → 设置 → 伪静态」)。改完执行 nginx -t,通过后 systemctl reload nginx。
依次检查:1) 规则是否在正确的 server 块里(server_name 匹配);2) 是否 reload 成功;3) root 是否指向程序入口目录;4) 程序后台是否已开启伪静态/固定链接;5) 宝塔等面板是否有另一份伪静态文件覆盖。
本页同时给出了 .htaccess 写法,需要 Apache 启用 mod_rewrite 并允许 AllowOverride All。
多语言站点还需要官方文档里的语言前缀规则;后台目录名安装时随机生成,记得替换 admin-dev。