一、Nginx的安装
1.1下载安装包
选择合适的安装包( http://nginx.org/en/download.html),下载完成后上传到服务器;
或者使用wget http://nginx.org/download/nginx-1.11.5.tar.gz命令直接下载
1.2安装准备
安装Nginx的依赖库
yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
解压安装包
# tar -zxvf nginx-1.11.5.tar.gz
编译配置:
1.默认配置
# ./configure
2.添加了https模块的配置
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
1.3安装
# make
# make install
Nginx别默认安装在 /usr/local/nginx 目录
二、配置启动命令
进入 /lib/systemd/system/
# cd /lib/systemd/system/
创建文件nginx.service文件
# vim nginx.service
写入
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启动命令列表:
systemctl start nginx.service 启动nginx服务
systemctl stop nginx.service 停止服务
systemctl restart nginx.service 重新启动服务
systemctl list-units —type=service 查看所有已启动的服务
systemctl status nginx.service 查看服务当前状态
systemctl enable nginx.service 设置开机自启动
systemctl disable nginx.service 停止开机自启动
三、启动配置
进入 /usr/local/nginx/conf
# cd /usr/local/nginx/conf
打开nginx.conf
# vi nginx.conf
修改为
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name 94.191.21.122 www.megitt.com;
# HTTP 转到 HTTPS
rewrite ^ https://www.megitt.com;
}
server {
listen 8080;
server_name localhost;
location / {
root /var/www/resource;
index index.html index.htm;
}
}
#HTTPS server
server {
listen 443 ssl;
server_name 94.191.21.122 www.megitt.com;
root /var/www;
# HTTPS 证书配置
ssl_certificate /usr/local/nginx/ssl/1575899_www.megitt.com.pem;
ssl_certificate_key /usr/local/nginx/ssl/1575899_www.megitt.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
# 配置根路径访问的文件
location / {
root /var/www/web/index;
index index.html index.htm;
}
location /loveone {
alias /var/www/web/love/type_1;
index index.html index.htm;
}
}
}
四、配置HTTPS
申请ssl证书放到 /usr/local/nginx/ssl 目录
五、配置多个站点
nginx.conf
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include /usr/local/nginx/vhosts/*;
}
创建vhosts文件夹
www.megitt.com.conf
#gzip on;
server {
listen 80;
server_name 94.191.21.122 www.megitt.com;
# HTTP 转到 HTTPS
rewrite ^ https://www.megitt.com;
}
#HTTPS server
server {
listen 443 ssl;
server_name 94.191.21.122 www.megitt.com;
root /var/www;
# HTTPS 证书配置
ssl_certificate /usr/local/nginx/ssl/1575899_www.megitt.com.pem;
ssl_certificate_key /usr/local/nginx/ssl/1575899_www.megitt.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
# 配置根路径访问的文件
location / {
root /var/www/web/index;
index index.html index.htm;
}
location /loveone {
alias /var/www/web/love/type_1;
index index.html index.htm;
}
location /lovetwo {
alias /var/www/web/love/type_2;
index index.html index.htm;
}
location /cc {
alias /var/www/web/11;
}
location /resource {
alias /var/www/resource;
index index.html index.htm;
}
}
resource.megitt.com.conf
#gzip on;
server {
listen 80;
server_name 94.191.21.122 resource.megitt.com;
# HTTP 转到 HTTPS
rewrite ^ https://resource.megitt.com;
}
#HTTPS server
server {
listen 443 ssl;
server_name 94.191.21.122 resource.megitt.com;
root /var/www;
# HTTPS 证书配置
ssl_certificate /usr/local/nginx/ssl/1575899_resource.megitt.com.pem;
ssl_certificate_key /usr/local/nginx/ssl/1575899_resource.megitt.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
# 配置根路径访问的文件
location / {
root /var/www/resource;
index index.html index.htm;
}
}
六、注意
root用来设置根目录,而alias用来重置当前文件的目录。