Nginx basic config on Ubuntu 20.04LTS. Nginx 配置不复杂,关键不天天使用,时间久了就忘记了,所以做个记录在这里,用的时候可以快速看一遍就行了。今天主要配置SSL证书和子域名。
- 做好域名解析。
- 申请阿里云免费证书。
- 下载证书并上传到服务器目录
/etc/nginx/cert/ - 配置文件
/etc/nginx/sites-available/default - 一级域名配置
server {
listen 80;
listen 443 ssl; #配置HTTPS的默认访问端口号为443。此处如果未配置HTTPS的默认访问端口,可能会造成Nginx无法启动。Nginx 1.15.0以上版本请使用listen 443 ssl代替listen 443和ssl on。
server_name www.abc.com; #将www.certificatestests.com修改为您证书绑定的域名,例如:www.example.com。如果您>购买的是通配符域名证书,要修改为通配符域名,例如:*.aliyun.com。
root /var/www/html;
index index.html index.htm index.php;
ssl_certificate cert/1234567_www.abc.com.pem; #将domain name.pem替换成您证书的文件名称。
ssl_certificate_key cert/1234567_www.abc.com.key; #将domain name.key替换成您证书的密钥文件名称。
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。
ssl_prefer_server_ciphers on;
location / {
root /var/www/html; #站点目录。
index index.html index.htm index.php;
# php support
location ~\.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
if ($server_port = 80){
return 301 https://$server_name$request_uri;
}
if ($scheme = http){
return 301 https://$server_name$request_uri;
}
error_page 497 https://$server_name$request_uri;
}
- 二级域名配置 based on folder
server {
listen 80;
listen 443 ssl;
server_name sub.abc.com;
root /var/www/backend;
index index.html index.htm index.php;
ssl_certificate cert/1234567_www.abc.com.pem;
ssl_certificate_key cert/1234567_www.abc.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。
ssl_prefer_server_ciphers on;
charset utf-8;
location / {
root /var/www/backend;
}
}
- 二级域名反向代理例子(比如springboot的jar程序daemon) on localhost:8080
server {
listen 80;
listen 443 ssl;
server_name api.abc.com;
root /var/www/html/sts;
index index.html index.htm index.php;
ssl_certificate cert/1234567_www.abc.com.pem;
ssl_certificate_key cert/1234567_www.abc.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。
ssl_prefer_server_ciphers on;
charset utf-8;
location / {
root /var/www/html/api;
index index.html index.htm index.php;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080;
}
}