В данной заметке я расскажу как быстро привязать ssl сертификат к nginx. Испытания мы проведем тестовом сервере с тестовым сертификатом, который запросим у Comodo.
Итак, для начала определимся что у нас есть. А есть у нас один nginx сервер который смотрит в Интернет. И, для простоты, один внутренний (с ip 10.10.10.10) для которого и будет создаваться стандартное и ssl подключение. Конечно, серверов может быть сколько угодно. Но мы с вами сейчас не будем усложнять.
First we need to create a CSR-certificate request. Free it can be done either directly from Comodo, or through the website http://www.freessl.su/. We will do through http://www.freessl.su/.
Configuration files from nginx I lie in the directory / etc / nginx. Create a subdirectory there ssl and go into it.
bash:
mkdir / etc / nginx / ssl
cd / etc / nginx / ssl
Next, you need to create a CSR request. To do this, first create a private key file with the following command:
bash:
openssl genrsa -des3 -out secure.website.ru.key 2048
If you want to create a key file without a password, enter the following command
bash:
openssl genrsa -out secure.website.ru.key 2048
Next, create a request file to generate the certificate. To do this, write the following command:
bash:
openssl req -new -key secure.website.ru.key -out secure.website.ru.csr
And fill the field. After that, the file will be generated /etc/nginx/ssl/secure.website.ru.csr. Copy the contents and go to the site http://www.freessl.su/. There fill the field name, phone, email, and paste the contents of the file secure.website.ru.csr in the CSR. Click Next, select the appropriate contact email.
After that, the mailbox will receive a letter from Comodo to confirm the creation of ssl certificate. This email will contain a confirmation code. Click on the link in the email and confirm.
After a while you will come with a certificate file and the file with intermediate certificates. Copy the contents in /etc/nginx/ssl/secure.website.ru.crt.
bash:
cat secure_website_ru.crt >> /etc/nginx/ssl/secure.website.ru.crt
cat secure_website_ru.ca_bundle >> /etc/nginx/ssl/secure.website.ru.crt
In forming this Certificate is completed. Proceed to configure nginx. In our case /etc/nginx/nginx.conf configuration is as follows:
bash:
-
user nginx;
-
worker_processes 1;
-
error_log /var/log/nginx/error.log;
-
pid /var/run/nginx.pid;
-
events {
-
worker_connections 2048;
-
}
-
http {
-
upstream www {
-
server 10.10.10.10 weight=1 max_fails=3 fail_timeout=120;
-
}
-
include /etc/nginx/mime.types;
-
default_type application/octet-stream;
-
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
-
‘$status $body_bytes_sent “$http_referer” ‘
-
‘”$http_user_agent” “$http_x_forwarded_for”‘;
-
access_log /var/log/nginx/access.log main;
-
sendfile on;
-
#Секция для стандартного подключение по 80 порту
-
server {
-
listen 80;
-
server_name secure.website.ru;
-
reset_timedout_connection on;
-
location / {
-
proxy_pass http://www/;
-
proxy_next_upstream error timeout invalid_header http_500 http_503;
-
proxy_set_header Host $host;
-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
-
proxy_redirect off;
-
proxy_connect_timeout 100;
-
}
-
}
-
# Секция для подключения по ssl
-
server {
-
listen 443 ssl;
-
server_name secure.website.ru;
-
access_log logs/ssl-access.log;
-
error_log logs/ssl-error.log;
-
ssl_certificate ssl/secure.website.ru.crt;
-
ssl_certificate_key ssl/secure.website.ru.key;
-
ssl_verify_depth 3;
-
keepalive_timeout 60;
-
location / {
-
proxy_pass http://test/;
-
proxy_next_upstream error timeout invalid_header http_500 http_5
-
proxy_set_header Host $host;
-
proxy_set_header X-Real-IP $remote_addr;
-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
-
proxy_set_header X-Forwarded-Proto https;
-
proxy_redirect off;
-
}
-
}
-
}
Save and restart nginx.
bash:
/etc/init.d/nginx reload
Go into the details of settings I do not want. About them is very well written in the official documentation nginx.
The only thing that I would like to stress that if you want to hang on one server several different ssl certificates on port 443, then simply add this configuration will not work:
-
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;
-
…
-
}
Which configuration does receive a certificate of the first server, ie www.example.com, regardless of the queried name server. This behavior is related to SSL. SSL-connection is established before the browser sends a HTTP-request and nginx does not know the name of the requested server. Consequently, it can only offer the certificate server by default.
Resolving this issue you can find here.
On this basic setup nginx laws.