I have 2 website. Let's say website A and B.
Both are using Magento.
Almost all of products in B also built-in in A.
Website B is going to be closed hence all of its matching products will be redirected to website A.
For other page (non-matching products & other url) will be redirected to A's homepage.
I created script to map matching products from B to A (put it under name mapping.txt), it contains around 20000 url.
To do the redirection, I'm using nginx HttpMapModule (http://wiki.nginx.org/HttpMapModule)
I was able to map it by using this conf:
map $uri $new{
default http://www.a.com/?ref=b;
include /some/path/mapping.txt;
}
server {
listen 80 ;
listen 443 ssl;
server_name www.b.com;
root /some/path/www/b;
#redirect b
rewrite ^ $new redirect;
location / {
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
.
.
.
}
The problem is, I want to be able to access B's admin page.
Because of the default map, I can not access http://www.b.com/admin anymore.
Then, I changed the conf:
map $uri $new{
default http://www.a.com/?ref=b;
include /some/path/mapping.txt;
}
server {
listen 80 ;
listen 443 ssl;
server_name www.b.com;
root /some/path/www/b;
location / {
#redirect b
rewrite ^ $new redirect;
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
.
.
.
}
By this conf, I still unable to access http://www.b.com/admin but I can access http://www.b.com/index.php/admin but the css and js are not loaded because request to (http://www.b.com/skin/..... and http://www.b.com/js/..... are redirected to http://www.a.com/?ref=b)
But this conf is also problematic, it won't redirect to A's homepage if I type http://www.b.com/index.php/customer/account/login
example content of mapping.txt:
/product-abc.html http://www.a.com/product-abc12.html;
/category-a/product-xyz.html http://www.a.com/product-xyz.html;
/category-b/category-d/product-123.html http://www.a.com/category-e/product-12z.html;
You can use a simple domain redirect on .htaccess for these generic cases...
RewriteCond %{HTTP_HOST} ^www.a.com$ [NC]
RewriteRule ^(.*)$ http://www.b.com/$1 [R=301,L]
Related
I'm trying to migrate a site under CodeIgniter from Apache to Nginx. My first tests show very good performances, so it worst the try.
But I can't find the correct Nginx configuration to replace those RewriteRules :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I found this recipe on Nginx website https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/
location / {
try_files $uri $uri/ /index.php;
}
It works in most cases, except when I have a trailing slash. Meaning
http://example.com/controller/param/
will call http://example.com/controller/function/index.php (and return a 404)
instead of http://example.com/index.php/controller/function/ ...as it usually do with Apache rewrite.
I tried to add a rewrite :
rewrite ^/(.*)/$ /$1 permanent;
But it redirect POST to GET, so I loose all my post data...
Any clue ?
Thanks
Check if this helps. Replace both occurrences of <source_directory>; with your correct value:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root <source_directory>;
rewrite_log on;
index index.html index.php;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME <source_directory>$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.htaccess
{
deny all;
}
error_page 404 /404.html;
location = /40x.html
{
}
error_page 500 502 503 504 /50x.html;
location = /50x.html
{
}
}
I want to do nginx setup for handing two project with same domain.
Example domain: example.com
Angular project should run with example.com
magento2 project should run with example.com/shop
I tried the code below, but its not working.
location /shop {
alias /var/www/www.example.com/shop/;
index index.html;
try_files $uri $uri/
autoindex on;
}
Can please someone help me to do this.
You should use official NGINX configuration sample as provided here.
Naturally, you will prefix all the Magento 2 locations with /shop/, for your specific case.
So you will end up with this kind of config:
server {
listen 80;
server_name example.com;
location / {
root /path/to/your/generated/angular/resources;
try_files $uri$args $uri$args/ /index.html;
}
# Magento 2 directives start here...
location ~* ^/shop/setup($|/) {
# ...
}
# The rest of Magento 2 directives...
}
You can start with the following config to serve your Applications:
server {
listen 80;
server_name example.com;
location / {
root /path/to/your/generated/angular/resources;
try_files $uri$args $uri$args/ /index.html;
}
location /shop {
root /path/to/shop/;
index index.html;
try_files $uri $uri/;
}
}
I'm not 100% sure if the shop route will work. Maybe you need to configure php to serve it. Therefore you can follow this official example.
If you want to serve also to www.example.com you can set server_name *.example.com (docs).
I can't get the content of /resources to be delivered directly through http (with caching etc) and everything else (/, /anything) to be redirected to https. This is the config as it stands now.
location /resources/ {
access_log off;
autoindex on;
expires 3d;
## No need to bleed constant updates. Send the all shebang in o$
## fell swoop.
tcp_nodelay off;
## Set the OS file cache.
open_file_cache max=3000 inactive=120s;
open_file_cache_valid 45s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
root /usr/share/nginx/www/resources;
try_files $uri $uri/;
}
location / {
rewrite ^(.*)$ https://$http_host$request_uri redirect;
if ($http_host !~ "^www\."){
rewrite ^(.*)$ https://www.$http_host$request_uri redirect;
}
}
First remove the line try_files $uri $uri/ from your location /resources/.
The way you define your location /resources block requires that the url request path ends with a trailing /
In other words requesting
your-domain/resources
will not work but
your-domain/resources/
will work.
I'm having some issues setting up a server with an SSL certificate. I was able to install the certificate just fine and restarted the nginx service. However, when I attempt to load my website, I see that all img, css and js files are being retrieved with http instead of https. This is a Magento website. Is there something wrong with my conf file?
server {
listen 80;
server_name www.my-domain.com;
return 301 $scheme://my-domain.com$request_uri;
}
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/ssl/my-domain/my-domain_com.pem;
ssl_certificate_key /etc/ssl/my-domain/my-domain_com.key;
access_log /var/log/nginx/magento.local-access.log;
error_log /var/log/nginx/magento.local-error.log;
server_name my-domain.com;
root /var/www/my-domain;
include conf/magento_rewrites.conf;
include conf/magento_security.conf;
# PHP handler
location ~ \.php {
## Catch 404s that try_files miss
if (!-e $request_filename) { rewrite / /index.php last; }
## Store code is defined in administration > Configuration > Manage Stores
fastcgi_param MAGE_RUN_CODE default;
fastcgi_param MAGE_RUN_TYPE store;
# By default, only handle fcgi without caching
include conf/magento_fcgi.conf;
}
# 404s are handled by front controller
location #magefc {
rewrite / /index.php;
}
# Last path match hands to magento or sets global cache-control
location / {
## Maintenance page overrides front controller
index index.html index.php;
try_files $uri $uri/ #magefc;
expires 24h;
}
rewrite ^/minify/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
rewrite ^/skin/m/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
location /lib/minify/ {
allow all;
}
}
maybe cause this is how they are called, if you need to serve every thing as https, i would create an empty server that listens on 80 and redirect to https
server {
# listen 80; delete this part
listen 443 ssl;
# the rest of the config
}
# add this server
server {
listen 80;
server_name example.com;
location / # or a more specific location '~ \.(jpg|css|js|jpeg|png|gif)' {
return https://example.com$request_uri;
}
}
or just fix the css location, it might be an absolute URL with http
How can I 301 rewrite mysite.com/page/ to mysite.com/page/index.html using nginx?
In Apache I had:
RewriteRule page/ http://mysite.com/page/index.html [R=301,L]
Thanks for help,
hydn
Try this settings:
location / {
rewrite /page/ http://mysite.com/page/index.html permanent;
...
}
I see from your comment to Sergiei that the '/page/' directory and '/page/index.html' does not actually exist and is rewritten elsewhere. So not surprising that Nginx gives a 404 not found.
What exactly should get served if a visitor requests '/page/index.html'? I.E., what does that get rewritten to?
If it is index.php?q=/page/index.html, then your config should be:
server {
# index directive should be in server or http block
# So no need to repeat in every location block
# unless specifically overriding it
index index.php index.html;
location / {
rewrite ^/page(/?)$ /page/index.html break;
try_files $uri $uri/ /index.php?q=$uri;
}
}
You could also use
server {
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
}
But there may be some issues with that. All depends on the detail of your application.