2016-09-30 134 views
1

我正在将站点从apache移动到nginx,并且坚持使用以下配置。配置nginx语言子目录

我有网站http://example.com/它显示主(英文)版本。 另外,我还有几种可以使用通讯录子目录打开的语言。 http://example.com/dehttp://example.com/frhttp://example.com/eshttp://example.com/es/(带有斜线)。

这些子目录是虚拟的(不存在),但应该从根目录打开相同的页面。一个php脚本处理语言表示。

现在英文网站工作正常,但是,其他语言不起作用。 我可以打开http://example.com/es/(仅适用于尾部字符)并打开主页面,但是无法访问所有其他页面(例如,http://example.com/es/test.html这是一个seo朋友url)。我已经在SO上回顾了很多类似的问题和答案,但其中没有一个是有帮助的。 这里是我的配置:

server { 
    .... 
    root /var/www; 
    index index.php index.html index.htm; 

    location/{ 
      rewrite ^/(de|fr|it|es)\/(.*)$ /$2; 
      try_files $uri $uri/ @fallback; 
    } 

    location @fallback { 
      rewrite ^(.*)$ /seo.php?$args last; 
    } 

    location ~* \.(jpeg|ico|jpg|gif|png|css|js|pdf|txt|tar|gz|wof|csv|zip|xml|yml) { 
      access_log off; 
      try_files $uri @static; 
      expires 14d; 
      add_header Access-Control-Allow-Origin *; 
      add_header Cache-Control public; 
      root /var/www; 
    } 

    location @static { 
      rewrite ^/(\w+)/(.*)$ /$2 break; 
      access_log off; 
      rewrite_log off; 
      expires 14d; 
      add_header Cache-Control public; 
      add_header Access-Control-Allow-Origin *; 
      root /var/www; 
    } 

    location /backend/ { 

      rewrite ^(.*)$ /backend/index.php last; 
    } 

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

它以前在Apache的工作如下:

RewriteRule ^(de|fr|it|es)\/(.*)$ $2 
RewriteCond %{REQUEST_URI} !^/(backend|template)/ 
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpeg|png|js|css|swf|php|ico)$ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteRule ^(.*)$ seo.php [L] 

请指教。谢谢。

回答

1

很明显,在应用重写之前,Apache正在将/es转换为/es/nginx不会这样做,除非该目录实际存在。

但是,通过调整正则表达式并使尾部斜线可选,可以很容易地进行修复。

尝试这种情况:

rewrite ^/(?:de|fr|it|es)(?:/(.*))?$ /$1; 

(:? )构建体是一种非捕获组。

编辑:

如果你想成为“明显”加最后的斜线,那么您将需要一个重定向。例如:

rewrite ^/(de|fr|it|es)$ /$1/ permanent; 
rewrite ^/(?:de|fr|it|es)/(.*)$ /$1 last; 
+0

现在,如果我尝试打开example.com/de它会打开一个英文版本代替,并且不添加尾部斜杠(我想要到达)。而语言子目录内的任何其他网址仍然无效。例如example.com/de/test.html(seo友好的网址)。 – Ssey

+0

我已添加重定向选项以追加尾部斜线。关于你的第二个问题,错误是什么,'/ seo.php'是否被执行? –

+0

谢谢你的帮助。目录现在运作。 URL重写也在起作用。问题似乎如下:如果我尝试访问不应该被重写的php脚本,例如example.com/de/api.php它似乎路由到seo.php以及404错误显示。如果我尝试访问example.com/api.php(没有语言目录),它可以工作。 – Ssey