centos 7.2 nginx https配置

centos版本:7.2

nginx版本:1.10.1

一、事前准备

1、下载Nginx

官网下载,或者其他wegt等形式下载

二、安装nginx

1、解压

tar -zvxf nginx-1.10.1.tar.gz 

2、安装依赖

yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

3、进入Nginx解压目录

cd nginx-1.10.1

4、安装模块

./configure --with-http_ssl_module --with-http_gzip_static_module

--with-http_ssl_module表示使用ssl模块,--with-http_gzip_static_module表示使用gzip模块,其它更详细的配置就要参考nginx的文档

5、make

#make
#make install

6、启动nginx

#/usr/local/nginx/sbin/nginx
#./nginx

停止命令:./nginx -s stop

程序位置:/usr/local/nginx/sbin/nginx 

配置文件位置:/usr/local/nginx/conf/nginx.conf

7、验证安装

打开浏览器,访问http://localhost/看看nginx的默认页面:Welcome to nginx!

三、Https配置

ssl证书由阿里云发布申请

1、申请证书并且下载

证书有两个文件文件分别为:xxxx.pem,xxxx.key

2、在Nginx目录中创建cert文件夹,将两个证书文件放到文件夹中

3、vim nginx.conf

添加一下内容,nginx将http访问重定向到https访问

其中http://localhost:xxxx/,是应用服务器

   server {
		listen 443;
		server_name 域名;
		
		ssl on;
		root html;
		index index.html index.htm;
		ssl_certificate   cert/xxxx.pem;
		ssl_certificate_key  cert/xxxx.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 / {  

			proxy_pass http://localhost:xxxx/; 
			
			error_page 504 = /50x.html;
		
		}
	}
	
	server {
		listen 80; 
		server_name 域名;
		# 跳转到HTTPS
		return	301 https://$server_name$request_uri;
	}

4、验证配置

访问域名即可

四、开机启动配置

centos 7以上是用Systemd进行系统初始化的,Systemd服务文件以.service结尾,比如现在要建立nginx为开机启动,这里我是用源码编译安装的,所以要手动创建nginx.service服务文件。

1、在系统服务目录里创建nginx.service文件

# vi /lib/systemd/system/nginx.service

插入内容

[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/www/lnmp/nginx/sbin/nginx -c /www/lnmp/nginx/conf/nginx.conf
ExecReload=/www/lnmp/nginx/sbin/nginx -s reload
ExecStop=/www/lnmp/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

2、设置开机启动

# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

3、其他命令

启动nginx服务
systemctl start nginx.service 
设置开机自启动
systemctl enable nginx.service
停止开机自启动
systemctl disable nginx.service
查看服务当前状态
systemctl status nginx.service
重新启动服务
systemctl restart nginx.service 
查看所有已启动的服务
systemctl list-units --type=service

猜你喜欢

转载自blog.csdn.net/u010375456/article/details/81123688
今日推荐