zoukankan      html  css  js  c++  java
  • How to Use Nginx to Redirect

     小结:

    浏览器地址栏: test.com 整站 url 变为 www.test.com

    server
    {
    listen 80;
    listen 443 ssl http2;
    server_name test.com;

    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    #HTTP_TO_HTTPS_START
    if ($server_port !~ 443){
    rewrite ^(/.*)$ https://$host$1 permanent;
    }
    #HTTP_TO_HTTPS_END
    ssl_certificate /www/server/panel/vhost/cert/test.com/fullchain.pem;
    ssl_certificate_key /www/server/panel/vhost/cert/test.com/privkey.pem;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    access_log /www/wwwlogs/test.com.log;
    error_log /www/wwwlogs/test.com.error.log;
    return 301 $scheme://www.test.com$request_uri;

    }

    注意:区别 


    rewrite
    proxy_pass 对地址栏没影响

    How to Use Nginx to Redirect to HTTPS, www/non-www and More! https://www.hostinger.com/tutorials/nginx-redirect/

    Nginx (pronounced engine-x) is a powerful open source high performing HTTP web server. It can work as a reverse proxy or POP3/IMAP proxy. It is the third most popular web server and well known for its enhanced performance, ease of use and configuration, stability and minimum resource utilization. That’s why in this tutorial, we’ll show you how to use Nginx to redirect traffic in different ways.

    According to Datanyze, Nginx 32% market share in the web server market. It supports a lot of renowned websites such as Github, Netflix, CloudFlare, Hulu,  Airbnb, Hulu, WordPress and many more.

    Redirection in Nginx

    The ability to forward the URL of the website to another address or point based on your criteria is an essential feature of the Nginx web server. An Nginx redirect is simple and easy to set up. Often users choose to redirect a page that has good SEO ranking. For instance when switching from a CMS to another platform. This will completely change your URL composition. So, to keep your current page with the good SERP position, you can reroute the old URL to the new page.

    In this tutorial, we will guide you through creating two kinds of Nginx redirect: permanent and temporary. Remember to have Nginx installed on your VPS.

    Temporary and Permanent Nginx Redirect Explained

    Temporary redirects is beneficial if a page location needs to change from one place to another location temporarily. The redirects response code 302 is used for designating the temporary movement of a page.

    If website maintenance is being performed, temporary redirects are used to notify the users that the website is unavailable. Another example is when you make temporary redirects of an incomplete page; you link that page to another point or the main page:

    Visitor–> Website Page–> Website is under maintenance

    On the other hand, a permanent Nginx redirect informs the web browser that it should permanently link the old page or domain to a new location or domain. To map this change, the redirects response code 301 is used for designating the permanent movement of a page. These kinds of redirects are helpful when the user wants to change the domain name and no longer wants a browser to access it.

    For example, when you wish to change the domain of your website or create a new page for an older one:

    Visitor–> Click www.devisers.in/home -> Redirected to www. devisers. in/home1

    Page Redirects in Nginx

    Remember, first you have to access your VPS through SSH. If you’re having trouble, check out our PuTTY tutorial.

    In Nginx, most redirects can be achieved with the help of inbuilt rewrite feature. This is the default feature that’s available on a clean installation of Nginx and can form both kinds of Nginx redirect – i.e. permanent and temporary. In its plain form, it takes a minimum of two cases i.e. old URL and new URL.

    It is simple and easy to redirect pages to a temporary or permanent location on Nginx web server. In the location /etc/nginx/sites-enabled/default you should paste in the following code, while changing the variables to suit your needs:

    Location path_pattern {
    rewrite ^/oldURL$ https://www.domainone.com/newURL redirect;
    }

    If you want to redirect the page to another link permanently, simple use “permanent” instead of “redirect” in the above command. Meanwhile, the path_patern is typically /index.html.

    Nginx Redirecting a Domain

    For redirecting one domain to another use the below command in the terminal:

    server {
          listen 80;
          hostname devisers.in www.devisers.in;
          rewrite ^ http://www.devisers.com$request_uri? permanent;
    }

    Here, we use two domains. The one we want to redirect – www.devisers.in, and the new one  – www.devisers.com.

    Nginx Redirect from HTTP to HTTPS (SSL)

    HTTP and HTTPS use different ports – HTTP port 80 and HTTPS port 443. Using HTTPS is much more helpful since it protects you from MITM attacks that can hijack your session. Remember, that for this method to work, you need to have an SSL already set up. So, to protect all the information sent between you and your visitors, it is beneficial to redirect all the requests coming from HTTP to HTTPS. For that, we can add this modification to the same file:

    server {
    listen 80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
    }

    Now all traffic for HTTP default server redirects to HTTPS.

    Nginx Redirect Specific Sites

    This is essential if you are using various sites or apps and want to redirect only a single site. Follow the steps below:

    server { 
    listen 80; 
    server_name devisers.in;
         return 301 https://devisers.in$request_uri; 
    }

    Here, we are redirecting the site http://www.devisers.in to https://www.devisers.in

    Redirect From www to non-www

    There are many times when you want your visitors to access the plain domain of your web page such as devisers.in instead of www.devisers.in. Although there are many options to redirect from www to non-www in Nginx, one of the easy way to do so is as described below:

    server {
        server_name www.devisers.in;
        return 301 $scheme://devisers.in$request_uri;
    }

    Important: This is permanent Redirect or “301 Redirect”.

    Restart the Nginx web server to put the changes into effect using the command:

    sudo systemctl restart Nginx

    If you wish to redirect from non-www to www, simply replace the website URL’s mentioned in the above command. Replace www.devisers.in with devisers.in and vice versa.

    Conclusion

    Nginx is one of the most powerful and easy to use web servers which allows you to make temporary and permanent redirects as described above.

    Now you know how to create Nginx redirects from HTTP to HTTPS, from www or Non-www or vice versa. Make sure you use the correct redirection types, as incorrect redirections will affect your search rankings. With the help of accurate redirects, you can leverage your current web presence while changing the site structure as required.

    We hope this tutorial helps you out! See you in the next one.

  • 相关阅读:
    【SpringFramework】Spring 事务控制
    Mini 学生管理器
    方法的重写override,重载overload。
    方法封装,属性调用以及设置。
    判断hdfs文件是否存在
    模拟(删除/远程拷贝)当前一周的日志文件
    2.上传hdfs系统:将logs目录下的日志文件每隔十分钟上传一次 要求:上传后的文件名修为:2017111513xx.log_copy
    使用定时器:在logs目录,每两分钟产生一个文件
    五个节点的hadoop集群--主要配置文件
    hadoop集群配置文件与功能对应解析
  • 原文地址:https://www.cnblogs.com/rsapaper/p/14953762.html
Copyright © 2011-2022 走看看