我的服務(wù)器使用的系統(tǒng)是CentOS 6,因此直接使用yum安裝nginx,其它系統(tǒng)以及版本可能有所差異。
使用yum安裝nginx
step1, 先要安裝nginx的yum源
設(shè)置源
rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
執(zhí)行完上面的命令后,我們使用命令yum info nginx
查看一下Nginx 軟件包信息.
[root@host nginx]# yum info nginx
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: repos.lax.quadranet.com
* elrepo-kernel: repos.lax-noc.com
* extras: mirror.fileplanet.com
* updates: mirror.scalabledns.com
已安裝的軟件包
Name : nginx
Arch : i686
Version : 1.14.2
Release : 1.el6.ngx
Size : 2.5 M
Repo : installed
From repo : nginx
Summary : High performance web server
URL : http://nginx.org/
License : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
: a mail proxy server.
可安裝的軟件包
Name : nginx
Arch : i386
Version : 1.12.2
Release : 1.el6.ngx
Size : 967 k
Repo : nginx
Summary : High performance web server
URL : http://nginx.org/
License : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
: a mail proxy server.
安裝
step2, 安裝nginx, 使用命令yum install nginx
安裝宴杀,等待安裝完成。
yum install nginx
檢查nginx 版本nginx -v
[root@host nginx]# nginx -v
nginx version: nginx/1.14.2
這個(gè)時(shí)候我們發(fā)現(xiàn)nginx已經(jīng)安裝成功了拾因。
查看nginx配置
使用yum安裝的nginx旺罢, 默認(rèn)配置文件放在/etc/nginx/nginx.conf
旷余,使用命令nginx -t
可以拿到配置文件的存放路徑;這個(gè)命令用于測試配置文件語法是否準(zhǔn)確無誤扁达。
[root@host nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
使用vim查看配置內(nèi)容如下:
.......
.......
http {
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;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
看最后一行include /etc/nginx/conf.d/*.conf;
便知nginx include 了./conf.d/
下面的所有配置文件正卧,因此我們也可以將配置文件放在./conf.d/
目錄下。
配置https站點(diǎn)
在配置Https 之前罩驻,我們需要準(zhǔn)備好購買的SSL證書文件穗酥,我使用的是阿里云提供的免費(fèi)證書护赊。
1惠遏、將證書文件傳到服務(wù)器,我使用的是scp
命令將證書拷貝到服務(wù)器的/etc/nginx/ssl_certs/
目錄下面】現(xiàn)在該目錄下有兩個(gè)文件节吮,xxx.pem
和 xxx.key
。
2判耕、在創(chuàng)建配置文件/etc/nginx/conf.d/
目錄下創(chuàng)建 xxx.conf
文件
touch /etc/nginx/conf.d/draw.lyan.me.conf
將如下配置copy到配置文件中
server {
listen 443 ssl;
server_name draw.lyan.me;
ssl_certificate /etc/nginx/ssl_certs/draw.lyan.me.pem;
ssl_certificate_key /etc/nginx/ssl_certs/draw.lyan.me.key;
#ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
server {
listen 80;
server_name draw.lyan.me; # 你的域名
# 強(qiáng)制跳轉(zhuǎn)https
rewrite ^(.*) https://$server_name$1 permanent;
}
最后重啟 nginx, 使用 service nginx restart
命令重啟透绩。
[root@host conf.d]# service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
訪問 https://draw.lyan.me ,(PS: 我的服務(wù)器配置了dns解析)壁熄,即可發(fā)現(xiàn)https已經(jīng)生效