Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Nginx deploys https sites and configures address rewriting

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how Nginx deploys the https website and configures address rewriting. I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.

Nginx is not only a high-performance website server and reverse proxy server, but also a mail proxy server such as IMAP, POP3, SMTP and so on. Nginx can be used as a website server to publish websites, and nginx can be used as a reverse proxy to achieve load balancing. This article describes how to deploy a https Web site using Nginx in a centos6.9 environment and configure address rewriting.

1. Environment preparation: one centos6.9 host, turn off firewall and Selinux

Install the dependency package: yum-y install openssl-devel pcre-devel gcc

To create a nginx user:

Useradd-M-s / sbin/nologin nginx # does not create a home directory for Nginx users, and there is no interactive shelltar-xf nginx-1.8.0.tar.gzcd nginx-1.8.0./configure-- user=nginx-- group=nginx-- prefix=/usr/local/nginx-- with-http_stub_status_module-- with-http_ssl_module (--with-http_ssl_module specifies the installation security module block, which must be installed when deploying a https website The / usr/local/nginx directory does not need to be created beforehand) make & & make installcd / usr/local/nginxls / usr/local/nginx

Conf # storing nginx configuration files logs # storing service logs and PID files html # storing sbin # executable main program directory for website pages

two。 Put the launcher link under the / usr/sbin/ path:

Ln-s / usr/local/nginx/sbin/nginx / usr/sbin/

Nginx command:

Nginx # start service nginx-s stop # close service nginx-s reload # reload configuration file nginx-t # test configuration file nginx-v # View version information nginx-V # View compilation options

Start the service: nginx

Test whether the website can be accessed properly through IP. The following page indicates that the website is configured successfully (there is only one default http page at this time):

3. Use openssl to generate a certificate and configure the https website:

Cd / usr/local/nginx/confopenssl genrsa-out my.key # generates the private key of the rsa algorithm

Openssl req-new-x509-key my.key-out my.crt # generates a subsignature certificate, which is equivalent to a public key

Modify the nginx configuration file to specify the location of the certificate:

Vim / usr/local/nginx/conf/nginx.conf... Server {listen 443 ssl; server_name www.test.com; ssl_certificate my.crt; # specifies the certificate location. By default, look for the private key location in the current directory. Location / {root / var/www/html; # specifies the root path of the web page file, which is separated from the http website path, so it is easy to distinguish index index.html;}}.

Reload the configuration file after modification: nginx-s reload

Mkdir-p / var/www/htmlecho "ssl test" > / var/www/html/index.html

4. Access authentication:

The effect of http access is as follows:

The effect of https access is as follows:

5. Configure http address rewriting to automatically jump to https when the client accesses http:

Vim / usr/local/nginx/conf/nginx.conf... ... Server {listen 80; server_name www.test.com; rewrite ^ (. *) $https://${server_name}$1 permanent; # redirects to https location / {root html; index index.html index.htm;} when it receives a http access request

When the modification is complete, reload the configuration file:

Nginx-s reload

6. Visit again to verify:

When you access a web page through the http protocol, it automatically jumps to https:

If the domain name is not resolved, add a hosts record and write the corresponding relationship between the domain name and IP in the hosts file.

The above is all the contents of the article "how Nginx deploys https sites and configures address rewriting". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report