Nginx环境301跳转

  • A+
所属分类:Linux 技巧

Nginx环境301跳转

方法1:

if ($scheme = http ) {
    return 301 https://$host$request_uri;
}

方法2:

server_name aichiyudemao.com www.aichiyudemao.com;
rewrite ^(.*) https://aichiyudemao.com$1 permanent

方法3:

if ($server_port = 80 ) {
    return 301 https://$host$request_uri;
}

方法4:

server_name aichiyudemao.com ;
return 301 https://$server_name$request_uri;

一个实际配置示例:

server {
        listen       80;
        root /www/web/aichiyudemao_com/public_html;
        server_name aichiyudemao.com www.aichiyudemao.com;
        index  index.html index.php index.htm;
        if ($scheme = http ) {
                return 301 https://$host$request_uri;
        }
        error_page  400 /errpage/400.html;
        error_page  403 /errpage/403.html;
        error_page  404 /errpage/404.html;
        error_page  503 /errpage/503.html;
        location ~ \.php(.*)$ {
                fastcgi_pass  unix:/tmp/php-71-cgi.sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $DOCUMENT_ROOT$fastcgi_script_name;
                fastcgi_param PATH_INFO $2;
                include fcgi.conf;
        }
        location ~ /\.ht {
                deny  all;
        }
        location / {
                 try_files $uri $uri/ /?$args;
        }
}

server {
        listen       443;
        root /www/web/aichiyudemao_com/public_html;
        ssl                  on;
        ssl_certificate      cert/aichiyudemao.com.pem;
        ssl_certificate_key  cert/aichiyudemao.com.key;
        ssl_prefer_server_ciphers on;
        ssl_session_timeout 10m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
        server_name aichiyudemao.com www.aichiyudemao.com;
        index  index.html index.php index.htm;
        error_page  400 /errpage/400.html;
        error_page  403 /errpage/403.html;
        error_page  404 /errpage/404.html;
        error_page  503 /errpage/503.html;
        location ~ \.php(.*)$ {
                fastcgi_pass  unix:/tmp/php-71-cgi.sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $DOCUMENT_ROOT$fastcgi_script_name;
                fastcgi_param PATH_INFO $2;
                include fcgi.conf;
        }
        location ~ /\.ht {
                deny  all;
        }
        location / {
                 try_files $uri $uri/ /?$args;
        }
}
百分购

发表评论

您必须才能发表评论!