Detailed steps for deploying https websites and configuring address rewriting in Nginx

Detailed steps for deploying https websites and configuring address rewriting in Nginx

Nginx is a high-performance website server and reverse proxy server, as well as an IMAP, POP3, SMTP and other mail proxy servers; 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 use Nginx to deploy https websites and configure address rewriting in the centos6.9 environment.

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

Install dependency packages: yum -y install openssl-devel pcre-devel gcc

Create nginx user:

useradd -M -s /sbin/nologin nginx #Do not create a home directory for the Nginx user, no interactive shell
tar -xf nginx-1.8.0.tar.gz
cd 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 of the security module, which must be installed when deploying https websites. The /usr/local/nginx directory does not need to be created in advance)
make && make install
cd /usr/local/nginx
ls /usr/local/nginx 

conf #Store nginx configuration files logs #Store service logs and PID files html #Store website pages sbin #Executable main program directory

2. Link the startup program to the /usr/sbin/ path:

ln -s /usr/local/nginx/sbin/nginx /usr/sbin/

nginx command:

nginx # Start the service nginx -s stop # Shut down the service nginx -s reload # Reload the configuration file nginx -t # Test the configuration file nginx -v # View the version information nginx -V # View the compilation options

Start service: nginx

Test the website through IP to see if it can be accessed normally. If the following page appears, it means the website configuration is successful (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/conf

openssl genrsa -out my.key #Generate a private key for the RSA algorithm 

openssl req -new -x509 -key my.key -out my.crt #Generate a sub-signature certificate, 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; #Specify the certificate location. By default, it will be searched in the current directory. ssl_certificate_key my.key; #Specify the private key location location / { 
  root /var/www/html; #Specify the root path of web page files, separate from the http website path, to facilitate the distinction index index.html;
  }
}

After the modification is completed, reload the configuration file: nginx -s reload

mkdir -p /var/www/html
echo "ssl test" >/var/www/html/index.html 

4. Perform access verification:

The effect of http access is as follows:

The effect of https access is as follows:

5. Configure http address rewriting so that the client automatically jumps to https when accessing http:

vim /usr/local/nginx/conf/nginx.conf
... ...
 server {
  listen 80;
  server_name www.test.com;
  rewrite ^(.*)$ https://${server_name}$1 permanent; #When receiving an http access request, redirect to https
  location / {
   root html;
   index index.html index.htm;
  }

After the modification is completed, reload the configuration file:

nginx -s reload

6. Visit again to verify:

When accessing a web page through the http protocol, it will automatically jump to https:

If the domain name is not resolved, please add a hosts record and write the correspondence between the domain name and IP into the hosts file.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • How to block and prohibit web crawlers in Nginx server
  • Nginx reverse proxy and load balancing practice
  • Detailed explanation of the best practice of Django+uwsgi+Nginx online
  • How to increase your web performance by 3 times by turning on a parameter in Nginx
  • How to use PHP to count the User Agent data of Nginx logs
  • How to add Nginx to system services in CentOS7
  • How to deploy multiple Vue projects under the same domain name using nginx and use reverse proxy
  • Python implementation example of monitoring differences in Nginx configuration files and sending email alerts
  • Detailed explanation of the working principle of nginx+php execution request
  • Explanation of Linux kernel optimization configuration for high-concurrency nginx server

<<:  Detailed explanation of this pointing problem in JavaScript

>>:  How to install MySQL database on Debian 9 system

Recommend

Teach you how to implement Vue3 Reactivity

Table of contents Preface start A little thought ...

Implementation of MySQL Multi-version Concurrency Control MVCC

Table of contents What is MVCC MVCC Implementatio...

A brief analysis of event bubbling and event capture in js

Table of contents 01-Event Bubbling 1.1- Introduc...

A Brief Analysis of Patroni in Docker Containers

Table of contents Create an image File Structure ...

A simple way to change the password in MySQL 5.7

This is an official screenshot. After MySQL 5.7 i...

How to show or hide common icons on the desktop in Windows Server 2012

Windows Server 2012 and Windows Server 2008 diffe...

Detailed explanation of server-id example in MySQL master-slave synchronization

Preface When we build a MySQL cluster, we natural...

Tips for using the docker inspect command

Description and Introduction Docker inspect is a ...

Specific use of exception filter Exceptionfilter in nestjs

Speaking of Nestjs exception filter, we have to m...

CSS sets the box container (div) height to always be 100%

Preface Sometimes you need to keep the height of ...

Ten Experiences in Presenting Chinese Web Content

<br /> Focusing on the three aspects of text...

Mac VMware Fusion CentOS7 configuration static IP tutorial diagram

Table of contents Install CentOS7 Configuring Sta...