How to configure https correctly in nginx
今天聊一下如何在nginx中配置https
在web应用开发中,为保证前端访问后端服务器的安全,需要使用https连接,现在来聊一下如何在nginx中配置https.
首先需要申请ssl证书。 在阿里云,腾讯云,华为云等云服务提供商的网站一般都会有免费ssl证书,申请一个即可;下面以华为云为例;
下载证书,会得到server.key和server.crt两个文件;在与nginx.conf同目录下创建ssl文件夹(名字任意), 把这两个证书放入刚创建的文件夹中;
在nginx.conf的server中增加如下配置:
server { listen 443; server_name www.test.com # 域名 ssl on; # 启用ssl功能 ssl_certificate ssl/server.crt; ssl_certificate_key ssl/server.key; ssl_session_timeout 5m; # 客户端可以重用会话参数的时间 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 使用的协议 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; # 配置加密套件 ssl_prefer_server_ciphers on; ... }
在配置443端口之前,需要先打开防火墙和443端口,以Centos7为例:
1) 开启防火墙: systemctl start firewalld 查看防火墙状态: systemctl status firewalld 2) 查看开通了哪些端口: firewall-cmd --list-ports 3) 开通443端口: firewall-cmd --zone=public --add-port=443/tcp --permanent 4) 重新加载下防火墙配置: firewall-cmd --reload 注意: 如果还有其它应用在运行,开启防火墙后,需要开通相应的端口,否则不能访问。
80端口重定向至443端口的配置,在nginx.conf的server上面增加如下的server:
server { listen 80; server_name www.test.com; rewrite ^(.*)$ https://www.test.com$1 permanent; }
验证配置是否正确:
执行 /usr/sbin/nginx -t
返回如下信息则表示配置成功:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
更多Nginx相关技术文章,请访问Nginx使用教程栏目进行学习!
The above is the detailed content of How to configure https correctly in nginx. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.
