简体   繁体   English

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

[英]redirect ALL traffic to https. NGINX

I am trying to redirect all traffic to https://example.com no matter what the user types in. 无论用户输入什么内容,我都试图将所有流量重定向到https://example.com

The following .conf file is very close but it does not catch http://example.com 以下.conf文件非常接近,但无法捕获http://example.com

what would you suggest that I change in order for ALL traffic to redirect to https://example.com ? 您会建议我做什么以使所有流量重定向到https://example.com

Thank you. 谢谢。

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;
    }
}

Add another server record for http://example.com . http://example.com添加另一个服务器记录。

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

Also, make sure that the main server-record only listens on port 443 , so add this to it: 另外,请确保主服务器记录仅侦听端口443 ,因此将其添加到它:

listen 443 ssl;

Use separate server blocks. 使用单独的服务器块。

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM