nginx configure HTTPS server
http://nginx.org/cn/docs/http/configuring_https_servers.html
Configuring HTTPS Servers
The translated content may be out of date. You can view the latest updates via the English version . +To configure the HTTPS host, you must open the SSL protocol in the server configuration block and specify the location of the server-side certificate and key files:
server { listen 443; server_name www.example.com; ssl on; ssl_certificate www.example.com.crt; ssl_certificate_key www.example.com.key; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!MD5; ... }Copy after loginThe server certificate is public and will be transmitted to every client connected to the server end. The private key is not public and needs to be stored in a file with restricted access. Of course, the nginx main process must have permission to read the key. The private key and certificate can be stored in the same file:
ssl_certificate www.example.com.cert; ssl_certificate_key www.example.com.cert;Copy after loginIn this case, access restrictions must also be set on the certificate file. Of course, although the certificate and key are stored in the same file, only the certificate will be sent to the client, not the key.
The ssl_protocols and ssl_ciphers directives can be used to force user connections to only introduce strong protocol versions and powerful encryption algorithms of SSL/TLS. Starting from version 1.0.5, nginx uses "
ssl_protocols" by default SSLv3 TLSv1
" and "ssl_ciphers HIGH:!aNULL:!MD5
", so only in previous versions, it makes sense to configure them explicitly. Starting from version 1.1.13 and 1.0.12, nginx uses "ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2
".The encryption algorithm in CBC mode is vulnerable to some attacks, especially the BEAST attack (see CVE-2011-3389). It can be adjusted to preferentially use RC4-SHA encryption through the following configuration Algorithm:
ssl_ciphers RC4:HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on;Copy after loginHTTPS server optimization
SSL operations consume CPU resources, so in a multi-processor system, multiple worker processes need to be started, and the number needs to be no less than the number of available CPUs. The SSL operation of CPU resources is the SSL handshake. There are two methods to minimize the number of handshake operations for each client: the first is to keep the client long connected and send multiple requests in one SSL connection; the second is to Reuse SSL session parameters in concurrent connections or subsequent connections to avoid SSL handshake operations. The session cache is used to save SSL sessions. These caches are shared between worker processes and can be configured using the ssl_session_cache directive. The 1M cache can store approximately 4000. session. The default cache timeout is 5 minutes, you can use ssl_session_timeout to increase it. Here is an example of configuration optimization for a 4-core system, using a 10M shared session cache:
SSL certificate chainSome browsers do not accept certificates signed by well-known certificate authorities, while other browsers do accept them. This is because the certificate issuance uses some intermediate certificate authorities, which are authorized by the well-known certificate authorities to issue certificates on their behalf. However, they themselves are not widely recognized, so some clients do not recognize them. In this case, the certificate certification authority provides a certificate chain package to declare the relationship between the well-known certification authority and itself. This certificate chain needs to be included. The package is combined with the server certificate into one file. In this file, the server certificate needs to appear at the front of the authenticator certificate chain: worker_processes 4; http { ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; server { listen 443; server_name www.example.com; keepalive_timeout 70; ssl on; ssl_certificate www.example.com.crt; ssl_certificate_key www.example.com.key; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!MD5; ...Copy after loginThis file needs to be referenced using the ssl_certificate directive:$ cat www.example.com.crt bundle.crt > www.example.com.chained.crtCopy after loginIf the server certificate and certification The order of the party certificate chain is wrong when merging, nginx cannot start normally, and the following error message will be displayed:server { listen 443; server_name www.example.com; ssl on; ssl_certificate www.example.com.chained.crt; ssl_certificate_key www.example.com.key; ... }Copy after loginBecause nginx first needs to use the private key to decrypt the server certificate, but what it encounters is the authenticator's. Certificates.Browsers usually save intermediate certification authorities that are certified by trusted certification authorities. Then when these browsers encounter the situation of using these intermediate certification authorities but do not include the certificate chain, they have already saved them. The information of the intermediate certification authority, so no error will be reported. You can use the
opensslcommand line tool to confirm that the server sent the complete certificate chain:
SSL_CTX_use_PrivateKey_file(" ... /www.example.com.key") failed (SSL: error:0B080074:x509 certificate routines: X509_check_private_key:key values mismatch)Copy after loginIn this example, the signee ("s") of the server certificate (#0) for www.GoDaddy.com is Signed by the issuing authority ("i"), which is the signee of the certificate (#1), then the issuing authority of the certificate (#1) is the signee of the certificate (#2), and finally the certificate (#2) is issued by ValiCert, Inc, a well-known issuing agency. The certificate of ValiCert, Inc is embedded in the browser and is automatically recognized by the browser (this passage is similar to the content of the British poem "In the House That Jack Built").If the authenticator certificate chain is not added, only the server certificate (#0) will be displayed.
Merge HTTP/HTTPS hostIf the functions of HTTP and HTTPS virtual hosts are consistent, you can configure a virtual host to handle both HTTP requests and HTTPS requests. The configuration method is to delete the ssl oncommand and add the parameter
ssl
to the *:443 port:$ openssl s_client -connect www.godaddy.com:443 ... Certificate chain 0 s:/C=US/ST=Arizona/L=Scottsdale/1.3.6.1.4.1.311.60.2.1.3=US /1.3.6.1.4.1.311.60.2.1.2=AZ/O=GoDaddy.com, Inc /OU=MIS Department/CN=www.GoDaddy.com /serialNumber=0796928-7/2.5.4.15=V1.0, Clause 5.(b) i:/C=US/ST=Arizona/L=Scottsdale/O=GoDaddy.com, Inc. /OU=http://certificates.godaddy.com/repository /CN=Go Daddy Secure Certification Authority /serialNumber=07969287 1 s:/C=US/ST=Arizona/L=Scottsdale/O=GoDaddy.com, Inc. /OU=http://certificates.godaddy.com/repository /CN=Go Daddy Secure Certification Authority /serialNumber=07969287 i:/C=US/O=The Go Daddy Group, Inc. /OU=Go Daddy Class 2 Certification Authority 2 s:/C=US/O=The Go Daddy Group, Inc. /OU=Go Daddy Class 2 Certification Authority i:/L=ValiCert Validation Network/O=ValiCert, Inc. /OU=ValiCert Class 2 Policy Validation Authority /CN=http://www.valicert.com//emailAddress=info@valicert.com ...Copy after loginBefore version 0.8.21, only the listening port with thedefaultparameter added can addName-based HTTPS hostssl
Parameters:<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">server { listen 80; listen 443 ssl; server_name www.example.com; ssl_certificate www.example.com.crt; ssl_certificate_key www.example.com.key; ... } </pre><div class="contentsignin">Copy after login</div></div>
If you configure multiple HTTPS hosts on the same IP, a very common problem will occur: server { listen 443; server_name www.example.com; ssl on; ssl_certificate www.example.com.crt; ... } server { listen 443; server_name www.example.org; ssl on; ssl_certificate www.example.org.crt; ... }Copy after login使用上面的配置,不论浏览器请求哪个主机,都只会收到默认主机
www.example.com
的证书。这是由SSL协议本身的行为引起的——先建立SSL连接,再发送HTTP请求,所以nginx建立SSL连接时不知道所请求主机的名字,因此,它只会返回默认主机的证书。最古老的也是最稳定的解决方法就是每个HTTPS主机使用不同的IP地址:
server { listen 192.168.1.1:443; server_name www.example.com; ssl on; ssl_certificate www.example.com.crt; ... } server { listen 192.168.1.2:443; server_name www.example.org; ssl on; ssl_certificate www.example.org.crt; ... }Copy after login带有多个主机名的SSL证书
也有其他一些方法可以实现多个HTTPS主机共享一个IP地址,但都有不足。其中一种方法是使用在“SubjectAltName”字段中存放多个名字的证书,比如
www.example.com
和www.example.org
。但是,“SubjectAltName”字段的长度有限制。另一种方式是使用主机名中含有通配符的证书,比如
*.example.org
。这个证书匹配www.example.org
,但是不匹配example.org
和www.sub.example.org
。这两种方法可以结合在一起——使用在“SubjectAltName”字段中存放的多个名字的证书,这些名字既可以是确切的名字,也可以是通配符,比如example.org
和*.example.org
。最好将带有多个名字的证书和它的密钥文件配置在http配置块中,这样可以只保存一份内容拷贝,所有主机的配置都从中继承:
ssl_certificate common.crt; ssl_certificate_key common.key; server { listen 443; server_name www.example.com; ssl on; ... } server { listen 443; server_name www.example.org; ssl on; ... }Copy after login主机名指示
在一个IP上运行多个HTTPS主机的更通用的方案是TLS主机名指示扩展(SNI,RFC6066),它允许浏览器和服务器进行SSL握手时,将请求的主机名传递给服务器,因此服务器可以知道使用哪一个证书来服务这个连接。但SNI只得到有限的浏览器的支持。下面列举支持SNI的浏览器最低版本和平台信息:
- Opera 8.0;
- MSIE 7.0(仅在Windows Vista操作系统及后续操作系统);
- Firefox 2.0和使用Mozilla平台1.8.1版本的其他浏览器;
- Safari 3.2.1(Windows版需要最低Vista操作系统);
- Chrome(Windows版需要最低Vista操作系统)。
通过SNI只能传递域名,但是,当请求中包含可读的IP地址时,某些浏览器将服务器的IP地址作为服务器的名字进行了传送。这是一个错误,大家不应该依赖于这个。为了在nginx中使用SNI,那么无论是在编译nginx时使用的OpenSSL类库,还是在运行nginx时使用的OpenSSL运行库,都必须支持SNI。从0.9.8f版本开始,OpenSSL通过
“--enable-tlsext” 配置选项加入SNI支持,从0.9.8j版本开始,此选项成为默认选项。当nginx被编译成支持SNI时,在使用“-V”选项运行时会显示如下信息:$ nginx -V ... TLS SNI support enabled ...Copy after login但是,当开启SNI支持的nginx被动态链接到不支持SNI的OpenSSL库上时,nginx会显示如下警告:
nginx was built with SNI support, however, now it is linked dynamically to an OpenSSL library which has no tlsext support, therefore SNI is not availableCopy after login兼容性
- 从0.8.21和0.7.62版本开始,使用“-V”选项运行nginx时,将显示SNI支持状态信息。
- 从0.7.14版本开始,listen指令支持
ssl
参数。- 从0.5.32版本开始,支持SNI。
- 从0.5.6版本开始,支持SSL会话缓存,并可在工作进程间共享。
- 0.7.65、0.8.19及以后版本,默认SSL协议是SSLv3、TLSv1、TLSc1.1和TLSv1.2(如果OpenSSL库支持)。
- 0.7.64、0.8.18及以前版本,默认SSL协议是SSLv2、SSLv3和TLSv1。
- 1.0.5及以后版本,默认SSL密码算法是
HIGH:!aNULL:!MD5
。- 0.7.65、0.8.20及以后版本,默认SSL密码算法是
HIGH:!ADH:!MD5
。- 0.8.19版本,默认SSL密码算法是
ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM
。- 0.7.64、0.8.18及以前版本,默认SSL密码算法是
ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP
。
作者: Igor Sysoev
编辑: Brian Mercer
翻译: cfsego
以上就介绍了nginx 配置HTTPS服务器,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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 Share Keyboard and Mouse Between Mac/PC Using Barrier You need to make sure that the computers you want to share your mouse and keyboard with are on the same network, and you'll be switching back and forth between different Macs during the initial setup. Get the latest version of Barrier here (DMG for Mac, exe for Windows) – Download it to each computer you want to be able to use your keyboard and mouse Install Barrier from the DMG (or use the exe to Windows) Copy to the /Applications folder on each Mac you plan to use it on and right-click Barr

After Java8-291, TLS1.1 is disabled, so that JDBC cannot connect to SqlServer2008 using SSL. What should I do? The following is the solution to modify the java.security file 1. Find the java.security file of jre. If it is jre, go to {JAVA_HOME}/jre/ In lib/security, for example????C:\ProgramFiles\Java\jre1.8.0_301\lib\security. If it is the Eclipse green installation-free portable version, search for java.security in the installation folder, such as????xxx\plugins \org

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

Keeping web servers load balanced is one of the key measures to prevent downtime. Using a load balancer is a reliable approach, with HAProxy being a highly regarded choice. Using HAProxy, you can accurately configure the load balancing method and support SSL passthrough to ensure the security of communication between the client and the server. It starts by exploring the importance of implementing SSL passthrough in HAProxy, followed by a detailed discussion of the steps required to implement this feature and an example for better understanding. What is SSL passthrough? Why is it important? As a load balancer, HAProxy accepts and distributes the load flowing to your web servers across configured servers. Load distribution is targeted to client devices and

MySQL: Introduction to SSL connection and summary of setup steps: MySQL provides SSL (SecureSocketsLayer) connection to encrypt the data transmitted between the client and the server. This article will introduce the concept and role of SSL connections, and provide steps and related code examples to set up SSL connections in MySQL. Introduction: As networks and data transmission continue to expand, data security becomes more and more important. By using an SSL connection we can add

Installing an SSL certificate on SharePoint is a critical step in securing your website and providing an encrypted connection. By following the correct installation steps, you can ensure the security of your website data, improve your ranking in search engines, and provide a better user experience for your visitors. Get an SSL Certificate Contact a trusted Certificate Authority (CA) to purchase an SSL certificate. Provide the required authentication and domain ownership verification information. After completing the verification process, you will receive the SSL certificate file. Prepare the Certificate File Open your SSL certificate file using a text editor. Copy the certificate contents to a new text file. Save the file as yourdomain.cer, making sure to change "yourdomain”

Nginx is a high-performance web server software and a powerful reverse proxy server and load balancer. With the rapid development of the Internet, more and more websites are beginning to use the SSL protocol to protect sensitive user data, and Nginx also provides powerful SSL support, making the security performance of the web server even further. This article will introduce how to configure Nginx to support the SSL protocol and protect the security performance of the web server. What is SSL protocol? SSL (SecureSocket

Use of NginxProxyManager and SSL certificates: To ensure website security, specific code examples are required. Summary: This article aims to introduce the use of NginxProxyManager and SSL certificates to ensure website security. This article will introduce the basic concepts and functions of NginxProxyManager, and use specific code examples to show how to configure an SSL certificate to ensure the security of the website. Introduction In the modern Internet environment, website security is of paramount importance. and
