Nginx 301 redirect from oldsite to newsite - magento

Below URLs and their header status code. Please note that redirection is happening. But in some cases I see 301 in header and some cases I am not able to see.
https://www.oldsite.com -> 301 found in header
https://oldsite.com -> 301 found in header
http://www.oldsite.com -> No 301 found in header
http://oldsite.com -> No 301 found in header
https://www.newsite.com - Target site
https://newsite.com -> 302 found in header
http://www.newsite.com -> No 301 found in header
http://newsite.com -> No 301 found in header
I have four configuration stated below. Is there anything wrong with any of these configurations. Please note that this is a magento site.
oldsite.com.nginx.conf
server {
listen ipaddress:80;
server_name oldsite.com www.oldsite.com;
root /home/oldsite/web/oldsite.com/public_html;
index index.php index.html index.htm;
location / {
return 301 https://www.newsite.com$request_uri;
}
include /home/oldsite/conf/web/nginx.oldsite.com.conf*;
}
oldsite.com.nginx.ssl.conf
server {
listen ipaddress:443;
server_name oldsite.com www.oldsite.com;
root /home/oldsite/web/oldsite.com/public_html;
index index.php index.html index.htm;
ssl on;
ssl_certificate /home/oldsite/conf/web/ssl.oldsite.com.pem;
ssl_certificate_key /home/oldsite/conf/web/ssl.oldsite.com.key;
location / {
return 301 https://www.newsite.com$request_uri;
}
newsite.com.nginx.conf
server {
listen ipaddress:80;
return 301 https://www.newsite.com$request_uri;
server_name newsite.com www.newsite.com;
root /home/newsite/web/newsite.com/public_html/pub;
index index.php;
autoindex off;
charset UTF-8;
error_page 404 403 = /errors/404.php;
add_header "X-UA-Compatible" "IE=Edge";
}
newsite.com.nginx.ssl.conf
server {
listen ipaddress:443 http2;
server_name newsite.com www.newsite.com;
root /home/newsite/web/newsite.com/public_html/pub;
index index.php;
autoindex off;
charset UTF-8;
error_page 404 403 = /errors/404.php;
add_header "X-UA-Compatible" "IE=Edge";
ssl on;
ssl_certificate /home/newsite/conf/web/ssl.newsite.com.pem;
ssl_certificate_key /home/newsite/conf/web/ssl.newsite.com.key;
}

To handle example.com and www.example.com differently, you should split your existing server block into two, and place the desired return statement into one of them.
For example:
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /home/newsite/conf/web/ssl.newsite.com.pem;
ssl_certificate_key /home/newsite/conf/web/ssl.newsite.com.key;
return 301 https://www.newsite.com$request_uri;
}
server {
listen 443 ssl http2;
server_name www.example.com;
ssl_certificate /home/newsite/conf/web/ssl.newsite.com.pem;
ssl_certificate_key /home/newsite/conf/web/ssl.newsite.com.key;
root /home/newsite/web/newsite.com/public_html/pub;
index index.php;
autoindex off;
charset UTF-8;
error_page 404 403 = /errors/404.php;
add_header "X-UA-Compatible" "IE=Edge";
...
...
...
}

Related

Why does this 404 time out?

Can someone explain why it can take a page 30 seconds to timeout? According to Chrome's debugger it is pending on a 404.
And that I don't get. If it is a 404, why doesn't it return page not found right away then?
The nginx config in question looks like this
# redirect http to https
server {
listen ${API_PORT} default_server;
listen [::]:${API_PORT} default_server;
server_name example-api.${SITE_SUFFIX};
return 301 https://$host$request_uri;
}
server {
set $indexhtml 'index.html';
# port to example on. Can also be set to an IP:PORT
listen 8443 ssl;
# sets the domain[s] that this vhost server requests for
server_name example.${SITE_SUFFIX};
ssl_certificate /certs/example.${SITE_SUFFIX}/fullchain.pem;
ssl_certificate_key /certs/example.${SITE_SUFFIX}/privkey.pem;
proxy_ssl_ciphers HIGH:!aNULL:!MD5;
client_max_body_size 4G;
keepalive_timeout 10;
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
root /var/www/frontend/public;
error_page 404 #404;
include conf.d/sites/content.conf;
# error page location redirect 302
location #404 {
return 302 /404;
}
}
server {
# port to example on. Can also be set to an IP:PORT
listen 8443 ssl;
# sets the domain[s] that this vhost server requests for
server_name sl.example.${SITE_SUFFIX};
ssl_certificate /certs/sl.example.${SITE_SUFFIX}/fullchain.pem;
ssl_certificate_key /certs/sl.example.${SITE_SUFFIX}/privkey.pem;
proxy_ssl_ciphers HIGH:!aNULL:!MD5;
client_max_body_size 4G;
keepalive_timeout 10;
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
error_page 404 #404;
include conf.d/sites/sh.conf;
# error page location redirect 302
location #404 {
return 302 /404;
}
}
Update
This is how it looks after the timeout.

403 forbidden after installing Namecheap’s PositiveSSL

After installing SSL certificate with Namecheap’s PositiveSSL on my server i’m getting 403 forbidden error page. in HTTP the page does work.
I’m developing a Laravel app on a Nginx host
here’s my /etc/nginx/sites-enabled/ssl.conf
listen 443;
ssl on;
ssl_certificate /etc/nginx/sites-available/nginx_bundle_guida_deltipo.crt;
ssl_certificate_key /root/azas.social.key;
server_name azas.social;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
location / {
root /var/www/visibilio/visibilio/public;
index index.html;
}
}
And the following is nginx.conf file:
##
# SSL Settings
##
client_max_body_size 1000m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
this is my etc/nginx/sites-available/
server {
listen 80;
listen [::];
root /var/www/visibilio/visibilio/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name in-visibili.org;
return 301 https://azas.social$request_uri;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri /index.php?$query_string;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
Any help would be highly appreciated

Nginx keeps bad redirect for firefox

i am trying to solve problem. I want to redirect all urls starting with www to non-www version of site. In chrome and opera, it works well.
But when i go to firefox and open http://www.example.com it starts downloading page (mime-type octet/stream), on https://www the connection is not reliable. In other browsers it set mime-type text/html.
server {
listen 80;
listen [::]:80;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 80;
listen [::]:80;
root /var/www/domain/www;
index index.php;
server_name example.com;
return 301 https://$server_name$request_uri;
}
server {
root /var/www/domain/www;
# Add index.php to the list if you are using PHP
index index.php;
server_name example.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args ;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

Nginx - redirect non ssl & non www to ssl & www

I've looked on Stack Overflow but every time I find a piece of code that people say would work, it unfortunately doesn't work for me.
I'm using Laravel on Forge and trying to redirect non www & non ssl to ssl+www.
It works. However, it does not redirect https://example.com. It redirects all other's: example.com or www.example.com or http://example.com or http://www.example to https://www.example.com except the one mentioned above.
I have no idea why this is happening.
Here's my Nginx file:
server {
listen 80;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 80;
server_name xxx.xxx.xxx.xxx;
return 301 $scheme://example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
root /home/forge/example.com/public;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/example.com/21671/server.crt;
ssl_certificate_key /etc/nginx/ssl/example.com/21671/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/example.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
#cache:
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 168h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
Because your first two servers only listen to the post 80 (non ssl).
Change them like this:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/example.com/21671/server.crt;
ssl_certificate_key /etc/nginx/ssl/example.com/21671/server.key;
server_name example.com;
return 301 https://www.example.com$request_uri;
}

Nginx fails to load static files after I declared them using location

I've configured Nginx as you can see:
server {
listen 443 ssl;
ssl on;
ssl_certificate /etc/nginx/ssl/bundle.crt;
ssl_certificate_key /etc/nginx/ssl/privateKey.key;
location /webmin/ {
proxy_pass http://127.0.0.1:10000;
}
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/bundle.crt;
ssl_certificate_key /etc/nginx/ssl/privateKey.key;
server_name localjob.it;
access_log off;
location / {
alias /webapps/sitoweb/;
}
Now if I go on mysite.com the page is loaded with the CSS, but if I add:
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 168h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
Now if I go on mysite.com the page can't load CSS.
I can't understand the reason!!
Nginx locations exclusive so your alias inslide root location doesn't applies to another locations. Also it's a bit misuse, just use root directive in server block.
server {
listen 80;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/bundle.crt;
ssl_certificate_key /etc/nginx/ssl/privateKey.key;
server_name localjob.it;
access_log off;
root /webapps/sitoweb;
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 168h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}

Resources