We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Im currently working on using the Babel tutorial here for multilingual sites:

    http://www.class-zec.com/en/blog/2011/seo-friendly-multilingual-websites-with-modx-and-babel.html

    But I am having a small issue with the rewrite rules provided in the guide since they are apache specific. I was wondering if someone with experience in nginx could provide nginx equivalent rules?

    These are the apache rules:
    # The Friendly URLs part
    # detect language when requesting the root (/)
    RewriteCond %{HTTP:Accept-Language} !^de [NC]
    RewriteRule ^$ en/ [R=301,L]
    RewriteRule ^$ de/ [R=301,L]
     
    # redirect all requests to /en/favicon.ico and /de/favicon.ico
    # to /favicon.ico
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(en|de)/favicon.ico$ favicon.ico [L,QSA]
     
    # redirect all requests to /en/assets* and /de/assets* to /assets*
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(en|de)/assets(.*)$ assets$2 [L,QSA]
     
    # redirect all other requests to /en/* and /de/*
    # to index.php and set the cultureKey parameter
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(en|de)?/?(.*)$ index.php?cultureKey=$1&q=$2 [L,QSA]
    


    Here is the content of my nginx conf file that currently works with friendly urls (but obviously does not work with babel unless the necessary changes are made)
    server {
                listen   80;
                server_name dummy.***.com;
                access_log /home/***/public_html/dummy.***.com/log/access.log;
                error_log /home/***/public_html/dummy.j***.com/log/error.log;
                root   /home/***/public_html/dummy.***.com/public/;
    
                location = /favicon.ico     { return 204; }
                location = /favicon.png     { }
    
                # to serve static files
                location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico)$ {
                    access_log        off;
                    expires           30d;
                    break;
                }
                # to serve static files
    
                # default PHP Behaviour
                location ~ \.php$ {
                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_index index.php;
                    include /usr/local/nginx/conf/fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME /home/***/public_html/dummy.***.com/public/$fastcgi_script_name;
                }
                # default PHP Behaviour
    
                location /  {
                    index  index.php index.html;
                                                            
                        # modx tweak http://modxcms.com/forums/index.php?topic=50615.0
                        if (!-e $request_filename) {
                            rewrite ^/(.*)$ /index.php?q=$1 last;
                        }
                        # modx tweak
    
                }
    
           }
    
    


    Any help is greatly appreciated.
      • 26555
      • 38 Posts
      Here is how I got my multilangual site working with Nginx:

      server {
              listen  *:80;
              server_name     domain.local www.domain.local;
              root /var/www/appdir;
              index index.php;
      
              ## add www. for SEO
              if ($host != 'www.domain.local') {
                      rewrite ^/(.*)$ http://www.domain.local/$1 permanent;
              }
      
              ## set expires headers on known static filetypes and deliver immediately
              location ~* \.(css|js|jpeg|jpg|gif|png|ico)$ {
                      expires 30d;
                      break;
              }
      
              ## filter out lang prefix, if present
              ## this block is optional, in case you have i18n URLs like: /de/about-us, /en/about-us, etc.
              location ~ /(de|en)/ {
                      if (!-e $request_filename) {
                              rewrite ^/(de|en)/(.*)$ /index.php?q=$2 last;
                      }
              }
      
              ## matches any query, since all queries begin with /
              location / {
                      if (!-e $request_filename) {
                              rewrite ^/(.*)$ /index.php?q=$1 last;
                      }
              }
      
              ## pass php scripts to fastcgi
              location ~ \.php$ {
                      try_files $uri =404;
                      fastcgi_pass    127.0.0.1:9000;
                      fastcgi_index   index.php;
                      include         /etc/nginx/fastcgi_params;
                      fastcgi_param   SCRIPT_FILENAME /var/www/appdir$fastcgi_script_name;
              }
      
              ## disable viewing .htaccess & .htpassword
              location ~ /\.ht {
                      deny  all;
              }
      }
      


      This was my original tweet: http://twitter.com/#!/sebwebdev/status/61001201797435392
        • 791
        • 46 Posts
        I'm struggling to get Nginx rewrite working for my particular setup and I wondered whether anybody could help:

        My MODx revo site uses a jQuery code snippet to pull in property data from an estate agency package. The format of the URL for pages that contain the jQuery snippet is https://mydomain.com/propertydetails/#details/&p=13733. The estate agency package writes the # and everything after it.

        Nginx is stripping the part of the URL before the #, so it's becoming https://mydomain.com/#details/&p=13733.

        My Nginx rewrite config looks like this:

        location / {
            try_files $uri $uri/ @rewrite;
        }
        location @rewrite {
            rewrite ^/(.*)$ /index.php?q=$1;
        }


        Does anyone know how I can adjust it to accommodate the URL example above? Sorry for my ignorance about rewrite syntax.
          Learning a little more each day.
        • The rewrite syntax looks fine. This sounds like you are having an issue with not setting the base href value for the page containing the integration with the real estate package. If this is set, it's possible that the javascript is not base href aware, in which case this might not work without modifications.
            • 791
            • 46 Posts
            Thanks for the reply, Jason. You're right, it was user error. Once I set the correct base href in the estate agency package it worked fine.
              Learning a little more each day.
              • 49993
              • 2 Posts
              Hi,

              i also had problems setting up Modx with nginx and babel for de/en multilanguage site. My starting point was basically from @sebwebdev but i had also another challenge which is serving a sinatra app under a folder (which is used in modx as well to call things from via a API)

              But here is my config (using nginx 1.6.2 + passenger 4.0.59 on debian wheezy): (i have a super simple second server for non-www which just redirects everything to www.)

              server {
                      listen 443 ssl;
                      server_name www.example.de;
              
                      charset utf-8;
              
                      ssl_certificate /etc/ssl/certs/wildcard_example_de_052014_nginx.crt;
                      ssl_certificate_key /etc/ssl/private/wildcard_example_de_052014.key;
              
                      ssl_session_cache shared:SSL:10m;
                      ssl_session_timeout  10m;
                      ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
                      ssl_ciphers 'EDH+CAMELLIA:EDH+aRSA:EECDH+aRSA+AESGCM:EECDH+aRSA+SHA384:EECDH+aRSA+SHA256:EECDH:+CAMELLIA256:+AES256:+CAMELLIA128:+AES128:+SSLv3:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!DSS:!RC4:!SEED:!ECDSA:CAMELLIA256-SHA:AES256-SHA:CAMELLIA128-SHA:AES128-$
                      ssl_prefer_server_ciphers on;
              
                      # 1-yr HSTS
                      add_header Strict-Transport-Security max-age=31536000;
              
                      root /var/www/modx;
                      index index.php;
                      passenger_enabled off;
              
                      # SinatraApp
                      location ~ ^/sinatraapp {
                              alias /var/www/sinatraapp_api/current/public$1;
                              passenger_base_uri /sinatraapp;
                              passenger_app_root /var/www/sinatraapp_api/current;
                              passenger_document_root /var/www/sinatraapp_api/current/public;
                              passenger_enabled on;
                      }
              
                      location ~ ^/(partner|integration) {
                              return 301 https://anothersite.example.de$request_uri;
                      }
              
                      location ~ ^/(de|en)/favicon.ico {
                              rewrite ^/(de|en)/favicon.ico$ /favicon.ico last;
                      }
              
                      location ~ ^/(de|en)/assets {
                              rewrite ^/(de|en)/assets(.*)$ /assets$2 last;
                      }
              
                      location ~ ^/(de|en)/ {
                              rewrite ^/(de|en)/(.*)$ /index.php?cultureKey=$1&q=$2 last;
                      }
              
                      ## matches any query, since all queries begin with /
                      location / {
                              if (!-e $request_filename) {
                                      rewrite ^/(.*)$ /index.php?q=$1 last;
                              }
                      }
              
                      location ~ \.php$ {
                              try_files $uri =404;
                              fastcgi_pass unix:/var/run/php5-fpm.sock;
                              fastcgi_index index.php;
                              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                              include fastcgi_params;
                              fastcgi_ignore_client_abort on;
                              fastcgi_param  SERVER_NAME $http_host;
                      }
              
                      location ~ /\.ht {
                              deny  all;
                      }
              
                      # Limit request body size
                      client_max_body_size 20m;
              
                      access_log /var/log/nginx/www_example_de_https.access.log;
                      error_log /var/log/nginx/www_example_de_https.error.log
              }
              • There is a context switcher plugin that doesn't require any server rewrites, it's all handled in the plugin. https://gist.github.com/gadamiak/3812853
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 49993
                  • 2 Posts
                  Quote from: sottwell at Mar 04, 2015, 10:02 AM
                  There is a context switcher plugin that doesn't require any server rewrites, it's all handled in the plugin. https://gist.github.com/gadamiak/3812853

                  I've ultimately figured that out too while researching for our nginx config. We cannot simply change the method these days, so i had to go the "hard" way. And i've posted that here for other people in the same situation.

                  For our next Modx installations we will also use the context switching plugin instead of Babel wink
                  • No. It works with babel. Just use it instead of Babel's plugin. I first discovered it in this comment to the Babel tutorial article. http://www.multilingual-modx.com/blog/2011/seo-friendly-multilingual-websites-with-modx-and-babel.html#comment-667763296
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org