OpenCart 3 nginx config for multi language - nginx

I have a config
location / {
try_files $uri $uri/ #opencart;
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
location #opencart {
rewrite ^/(.+)$ /index.php?_route_=$1 last;
}
it works fine, but I am want to add multi-language support from URL like:
location /en {
rewrite ^/en/([^?]*) /index.php?_route_=$1&lang=en break;
}
location /es {
rewrite ^/es/([^?]*) /index.php?_route_=$1&lang=es break;
}
Get language as the first URL param, and put it as get parameter &lang=es to index.php.
How correctly do that?

I don't think there is a way to do that multilingual postfix through nginx (or even apache) configs. There are a lot of connections and routing around the main HTTP_SERVER (or HTTPS_SERVER) global constant. Maximum what will you get - 301 redirect on every page loading. Which is unacceptable for search engines.
I suggest you to try tree different ways to solve this tusk:
Rewrite system/library/url.php or
catalog/controller/startup/startup.php. Don't have the exact code, but if you are familiar to php - the routing of URL you can find in
these files. Nice manual here
How to set language through url in opencart
Use some free or paid modules from marketplace by query "language code in url".
If You are using SEO URL - there are multilingual urls on each product, category etc. You don't need postfix with these, all URLs could multilingual and unique.
If you don't have these functions - just download it from here, it's free https://www.opencart.com/index.php?route=marketplace/extension/info&extension_id=32788

Related

rewrite rules for nginx and Codeigniter

I have implemented a php application in codeigniter and now want to deploy it to the nginx server. Before deploying I checked my nignx configuration on my localhost using MAMP server. It is working correctly. But, this configuration is not working on the live server. As a beginner in nginx, I am not understanding where is the mistake here. In live server, I can not write in the main nginx.conf file. I have a separate configuration file like "abc" for my application "abc". And all my application files are under "abc/xyz" directory. Here is my sample confuguration,
location /abc {
root /srv/www/htdocs/apps/;
index index.html index.htm index.php;
location /xyz {
try_files $uri $uri/ /abc/xyz/index.php;
}
location ~ \.php(\/(\w+))*$ {
try_files $uri =404;
rewrite (.+)\.php(\/(\w+))*$ $1.php break;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Here, I can see my welcome page https://myapplication/abc/xyz. But if I want to navigate other pages like https://myapplication/abc/xyz/other_pages, it is showing "404 Page not found". I have checked the other solutions but none of them is not working in this case. Thanks in advance for the help!
The location /xyz block is nested within the location /abc block. The nested block is required to precess URIs with a prefix of /abc/xyz.
If there are other regular expression location blocks surrounding your location /abc block, you should use the^~` modifier.
For example:
location ^~ /abc {
...
location /abc/xyz {
...
}
...
}
See this document for more.
Sorry for the late answer. It was actually very silly mistake. My controller page name was in small character. This is why it was not working. My configuration is okay. The first letter of the controller page should be in capital character. For example, my controller name is Home. So my php file name must be Home.php not home.php.

Conditional rewriting, how to manage PHP scripts?

I need to create an API gateway that I can't test... But the problem is about manage PHP scripts.
What I need
all files HTML or PHP
special names as my1 and issn redirect to localhost at port 2018
special names as my2 and my3 redirect to ETC.php
Explanation
Pseudocode with details of "What I need",
if ($uri exists) {
if extension is .php use it with php7.0-fpm.sock
else use it as static page;
} else
try the #proxy_rewrite_engine;
#proxy_rewrite_engine =
if (regex ^\(my[23])$ use
ETC.php;
elseif (regex ^/(\d+)/my1$ use
http://127.0.0.1:2018?type=int&val=$1
elseif ^/([0-9]+\-\d+[Xx]?)/issn$ use
http://127.0.0.1:2018?type=str&val=$1
else
say error;
What I try
My problematic solution, please show a real solution, translating the "Explanation" section into concrete and correct NGINX script code.
... Below, my wrong-NGINX-script for clues and inspiration, it is not the solution... Need to use if instead neasted location? The fastcgi_param is valid? Can I group locations?
server {
server_name test.mytest.news;
root /var/www/test;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ #proxy;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi.conf; # without SCRIPT_FILENAME
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location #proxy {
rewrite ^\?(my[23])$ $document_root/ETC.php?cmd=$1
last;
rewrite ^/(\d+)/my1$ ?type=int&val=$1
break;
rewrite ^/([0-9]+\-\d+[Xx]?)/issn$ ?type=str&val=$1
break;
proxy_pass http://127.0.0.1:2018;
}
include snippets/ssl-test.mytest.news.conf;
} #end server
NOTE for #cnst comment: Suppose that what I need when say "all files HTML or PHP" = the try_files must to try other files tham index.htm, can be folders, imagens and other files.
If the desire is to have "a script 100% reliable, by its construction (not by testing)", then putting multiple independent issues in a single question is probably not the best approach.
Your rationale behind having to check existence of files prior to passing the request to a separate backend is not entirely clear, especially if the request is matching a given regular expression, like issn$. Best approach would be to use separate location directives for each distinct handling of the case.
More specifically, the following is also wrong with your approach: the regular expression in rewrite ^\?(my[23])$ is unlikely to match any request, as all external requests start with a slash.
Likewise, as per https://serverfault.com/a/864778/110020, the replacement string in rewrite ^/(\d+)/my1$ ?type=int&val=$1 is unlikely to be correct; in fact, when I've tried your code as above, the following is the 500 Internal Server Error I've gotten, as per nginx/error.log:
2017/07/24 08:46:22 [error] 45776#0: *2 the rewritten URI has a zero length, client: 127.0.0.1, server: , request: "GET /44444/my1 HTTP/1.1", host: "localhost:
This happens because you indeed rewrite URI to become an empty one — the question mark, ?, and anything after it, is generally not part of URI in request processing within nginx, so, as such, the URI is indeed entirely empty. Solution: at least put a slash in front of ? (i.e., /?id=), or, better yet, a full name of the script that must be activated (especially since this is merely an internal redirect).
Nginx works good with large configs. Bad idea optimize nginx.conf.
Do it simple, it'll work hard:)
I think config like this will work fine:
# Try to load existing files
location / {
try_files $uri $uri/ #rewriteIt;
}
# Handle all requests except two other locations below
location #rewriteIt {
# Send it to index.php
rewrite ^(.*)$ /index.php?cmd=$1 last;
}
# This RegExp location will rewrite to ETC.php
# It is more priority, than "/" and #rewriteIt
location ~ ^(my2|my3) $ {
rewrite ^(.*)$ /ETC.php?cmd=$1 last;
include /etc/nginx/fastcgi.conf; # without SCRIPT_FILENAME
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# This RegExp location will proxy to 2018 port
# It is more priority, than "/" and #rewriteIt
location ~ ^(my1|issn) $ {
proxy_pass http://127.0.0.1:2018;
}
# This location handles all php files to php-fpm
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi.conf; # without SCRIPT_FILENAME
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

Get WordPress installation working with nginx in subdirectory

Basically I have a site, called example.com. Within this site, there is a WordPress-Blog, wich should be accessible under "example.com/blog/". The actual directory where the blog is installed is not "/blog/", but "/blog/de/", as this project has multiple stand-alone-blogs for different languages.
The problem is, that WordPress cannot access to wp-content and wp-include files, because WordPress tries to load css (and other) files via example.com/blog/wp-content/....
So i want to get my Blog running under "example.com/blog", but all WordPress-Files should be loaded from /blog/de/.
This is my nginx-vhost-config for /blog-Location:
location /blog {
index index.php;
try_files $uri $uri/ /de/index.php?$args;
rewrite ^(/wp-content/.*)$ /de/wp-content/$1 last;
rewrite ^/blog/(.*)+$ /blog/de/index.php?$1;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
I am totally new to nginx, so every possible solution is welcome!
Essentially you need to alias the location /blog to the path /blog/de. The complication is the way in which PHP files are handled, particularly when other applications are hosted within the same domain.
First internally rewrite /blog to /blog/de:
location /blog {
rewrite ^/blog(.*)$ /blog/de$1 last;
}
Then implement the /blog/de location with a nested location to handle PHP:
set $wordpress /blog/de/index.php;
location ^~ /blog/de {
# root inherited from server container???
index index.php;
try_files $uri $uri/ $wordpress;
location ~ \.php$ {
try_files $uri $wordpress;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass ...;
}
}
I make /blog/de a priority prefix location, to avoid a conflict with any other php location. But that would not be necessary if this is the only application within this server block.

Certain URLs don't go through PHP-FPM

I'm having a lot of trouble setting up my nginx server with my PHP RESTful API. I have the following blocks in my server{} block:
location / {
rewrite ^/v1/* /v1/api.php last;
rewrite ^/* /index.php last;
}
location * .*\.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
However.. these seem not to be functioning properly. Basicly, I want every URL starting with /v1/ to be rewritten to /v1/api.php, and anything else to /index.php.
This seems to be working partially. Sometimes it actually does go to api.php, but sometimes it just seems to download the file instead of processing it through PHP-FPM. How would I fix this?
Downloading a file means not being passed to the php engine, so I assume the problem is in that block definition
replace
location * .*\.php$ {
with
location ~ \.php$ {
And probably it will work.

WordPress 3.0 & nginx - permalink, 404 problem

I've installed nginx, FastCGI and PHP on my server. WordPress 3.0 installed after a bit of a monster battle, but it's installed and working well.
However, when I change the permalink settings to anything other than default, I get 404 errors on every post, article and page.
I understand that this is something to do with nginx not supporting .htaccess and WordPress getting confused with where to go when a page is requsted.
I've tried a few rewrites in the nginx conf files and even the nginx compatibility plugin; neither have worked. With one rewrite I managed to stop the 404 errors, but instead of WordPress finding the post I was after I merely got my PHP confirmation page. Bah.
Forums are littered with people with similar issues. Does anyone have a solution?
On your location / block,
add this and remove any non-specific rewrite rules:
try_files $uri $uri/ /index.php;
If wordpress is on another directory besides the root, instead of having
if (!-e $request_filename) {
rewrite ^/wordpress/(.+)$ /wordpress/index.php?q=$1 last;
}
You can have:
location /wordpress {
try_files $uri $uri/ /wordpress/index.php?$args;
}
This page has exactly the same concept. I should have read and tried it first: nginx rewrite rule under a subdirectory
After much pain:
# if filename doesn't exist, take the request and pass to wordpress as a paramater
if (!-e $request_filename) {
rewrite ^/wordpress/(.+)$ /wordpress/index.php?q=$1 last;
}
If the requested file does not exist, pass it to index.php. It's a bit slow and I think I might try and not use a query, but it does work... :)
Have you tried the nginx Compatibility plugin?
Plus ElasticDog seems to provide a fairly concise article on getting WP working with nginx - which includes getting pretty permalinks to work.
Here's another article that seems to deal specifically with nginx rewrite rules for WordPress.
This was how I solved my permalinks in my wordpress blogs in dreamhost.
Inside the folder /home/ftpusername/nginx/example.com/ (if you don't have it, create it)
created the file
nginx.conf with the following content
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
restarted the nginx
/etc/init.d/nginx reload
Some notes:
ftpusername and example.com MUST be changed according to your system.
That was it!
Good luck for u all.
this does not work if you are using location other than / like:
~ .php$, what i meant to say that pretty link will work but your graphics will be all over the place. so what you need is exactly stated below.
http://www.pearlin.info
location ~ \.php$
{
try_files $uri $uri/ /index.php?$uri&$args;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?url=$1 break;
}
I did the following..
in the folder
/home/userrunningnginx/nginx/domain.com
I have:
default.conf (file)
include /home/neukbaarofnietps/nginx/neukbaarofniet.com/drop;
drop (file)
# Rather than just denying .ht* in the config, why not deny
# access to all .invisible files
location ~ /\. { deny all; access_log off; log_not_found off; }
nginx.conf (file)
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
WORDPRESS-NGINX.CONF (file)
#######################
# WP Super Cache
# if the requested file exists, return it immediately
if (-f $request_filename) {
break;
}
set $supercache_file '';
set $supercache_uri $request_uri;
if ($request_method = POST) {
set $supercache_uri '';
}
# Using pretty permalinks, so bypass the cache for any query string
if ($query_string) {
set $supercache_uri '';
}
if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
set $supercache_uri '';
}
# if we haven't bypassed the cache, specify our supercache file
if ($supercache_uri ~ ^(.+)$) {
set $supercache_file /wp-content/cache/supercache/$http_host$1/index.html;
}
# only rewrite to the supercache file if it actually exists
if (-f $document_root$supercache_file) {
rewrite ^(.*)$ $supercache_file break;
}
# all other requests go to Wordpress
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
Adding this block to your nginx.conf should solve the issue:
if (!-e $request_filename) {
rewrite ^/wordpress_dir/(.+)$ /wordpress_dir/index.php?q=$1 last;
}
Hope this helps.
Good luck.

Resources