We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17403 ☆ A M B ☆
    • 183 Posts
    Sample NGINX Configuration for Fancy URL + Babel


    ###################
    # Redirect domain.tld to www.domain.tld
    ###################
    #server {
    #	listen 80;
    #	listen [::]:80;
    #
    #	server_tokens off;
    #
    #	server_name domain.tld;
    #	return 301 $scheme://www.domain.tld$request_uri;
    #}
    ###################
    
    server {
    	listen 80;
    	listen [::]:80;
    	listen 443 ssl;
    	listen [::]:443 ssl;
    
    	server_tokens off;
    	
    	ssl_certificate /etc/cert/www.domain.tld/fullchain.pem;
    	ssl_certificate_key /etc/cert/www.domain.tld/privkey.pem;
    
    	root /var/wwwrot;
    
    	access_log /var/log/www.domain.tld-access.log;
    	error_log /var/log/www.domain.tld-error.log;
    
    	# Add index.php to the list
    	index index.html index.htm index.php;
    
    	#if domain.tld redirected to www.domain.tld, use the second one.
    	server_name domain.tld www.domain.tld;	
    	#server_name www.domain.tld;
    
    	# Error Page (change to MODx resource url if needed)
    	error_page 401 /401.htm;
    	error_page 402 /402.htm;
    	error_page 403 /403.htm;
    	error_page 404 /404.htm;
    	error_page 500 /500.htm;
    	error_page 503 /503.htm;
    
    	# Favicon: noisy favicon.ico off
    	location ~* favicon\.ico {try_files $uri /favicon.ico; access_log off; log_not_found off;}
    
    	# Core folder should be protected
    	location /path/to/core/ {
    		deny all;
    		return 404;
    	}
    
    	# Control cache for Manager js/css/gif/jpg/png if needed
    	location ~ ^/path-to-manager/.*\.(js|css)$ {try_files $uri $uri/ =404; expires 1d; add_header Cache-Control "private";}
    	location ~ ^/path-to-manager/.*\.(gif|jpg|jpeg|png)$ {try_files $uri $uri/ =404; expires 30d; add_header Cache-Control "private";}
    
    	location /download/ {try_files $uri  =404; expires -1; add_header Pragma "no-cache";}
    	location /images/ {try_files $uri $uri/ =404; expires 30d; add_header Cache-Control "public"; add_header Pragma "Public";}
    	location /assets/ {try_files $uri $uri/ =404; expires 30d; add_header Cache-Control "public"; add_header Pragma "Public";}
    
    
    	###################
    	#BABEL ON
    	###################
    	# Redirect root to /en/ home in English
    	location = / {return 301 $scheme://$host/en/;}
    
    	# Redirect /de or /en without end slash
    	location = /de {return 301 $scheme://$host/de/;}
    	location = /en {return 301 $scheme://$host/en/;}
    
    	# MODX babel /de/ and /en/
    	location /de/ {
    		rewrite ^/de/(.*)$ /index.php?cultureKey=de&q=$1 last;
    	}
    
    	location /en/ {
    		rewrite ^/en/(.*)$ /index.php?cultureKey=en&q=$1 last;
    	}
    	###################
    	#BABEL OFF
    	###################
    
    
    	# Root dir (default, if not matching one configuration above)
    	location / {
    		expires 1h;
    		add_header Pragma public;
    		add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    
    		# First attempt to serve request as file, then
    		# as directory, then fall back to load modx index.php.
    		
    		# try_files $uri $uri/ =404;
    		try_files $uri $uri/ @modxroot;
    	}
    
    	location @modxroot {
    		rewrite ^/(.*)$ /index.php?q=$1&args;
    	}
    
    	location ~* \.htm$ {
    		expires 1h;
    		add_header Pragma public;
    		add_header Cache-Control "public";
    	}
    
    	location ~ \.php$ {
    		include snippets/fastcgi-php.conf;
    		fastcgi_pass unix:/var/run/php5-fpm-mtweb.sock;
    	}
    
    	# deny access to .htaccess, .git and ht.access files
    	location ~ /\.ht {deny all;}
    	location ~ /\.git {deny all;}
    	location ~ ht\.access {deny all;}
    }
    
    [ed. note: lokamaya last edited this post 10 years, 8 months ago.]
      zaenal.lokamaya
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I prefer to use a plugin that does not require server rewrites. There are several routing plugins that work with OnPageNotFound.

      https://gist.github.com/gadamiak/3812853
      https://modx.today/posts/2015/05/using-xrouting-for-multilingual-websites-in-modx
        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
        • 17403 ☆ A M B ☆
        • 183 Posts
        Bookmarked. Thanks Susan... [ed. note: lokamaya last edited this post 10 years, 8 months ago.]
          zaenal.lokamaya