将所有流量重定向到https。 NGINX

时间:2016-09-01 18:01:04

标签: nginx

无论用户输入什么内容,我都会尝试将所有流量重定向到https://example.com

以下.conf文件非常接近,但它没有捕获http://example.com

您建议我更改以便将所有流量重定向到https://example.com

谢谢。

server {
    listen 80;
    listen 443;
    server_name www.example.com;
    return 301 https://example.com$request_uri;
}

server {
   server_name example.com;
   access_log /var/log/nginx/example-access.log;
   error_log /var/log/nginx/example-error.log;
   root /var/www/html/web;

   index index.html index.htm index.php;

   location / {
       try_files $uri $uri/ /index.php$uri?$args;
   }

   rewrite ^/backend\.php/?(.*)$ /$1 permanent;

    location /admin {
        index admin content backend.php;
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        rewrite ^(.*)$ /backend.php/$1 last;
    }

    location ~ "^(.+\.php)($|/)" {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass   127.0.0.1:9000;
        include        fastcgi_params;
    }
}

2 个答案:

答案 0 :(得分:1)

http://example.com添加另一个服务器记录。

server {
    listen 80;
    server_name example.com;
    return 301 https://example.com$request_uri;
}

此外,请确保主服务器记录仅侦听端口443,因此请将其添加到其中:

listen 443 ssl;

答案 1 :(得分:0)

使用单独的服务器块。

server {
    listen 80;
    server_name www.example.com
                example.com;
    return 301 https://example.com$request_uri;
}
server {
    listen 443;
    server_name www.example.com;
    return 301 https://example.com$request_uri;
}
server {
   server_name example.com;
   access_log /var/log/nginx/example-access.log;
   error_log /var/log/nginx/example-error.log;
   root /var/www/html/web;

   index index.html index.htm index.php;

   location / {
       try_files $uri $uri/ /index.php$uri?$args;
   }

   rewrite ^/backend\.php/?(.*)$ /$1 permanent;

    location /admin {
        index admin content backend.php;
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        rewrite ^(.*)$ /backend.php/$1 last;
    }

    location ~ "^(.+\.php)($|/)" {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass   127.0.0.1:9000;
        include        fastcgi_params;
    }
}