1. 程式人生 > >ubuntu 伺服器nginx配置https

ubuntu 伺服器nginx配置https

1.安裝nginx:

基於APT源安裝

sudo apt-get install nginx

安裝好的檔案位置:

/usr/sbin/nginx:主程式

/etc/nginx:存放配置檔案

/usr/share/nginx:存放靜態檔案

/var/log/nginx:存放日誌

2.獲取ssl證書

騰訊雲申請免費的ssl 證書,nginx環境下證書檔案

2_www.yangbb.cn.key

1_www.yangbb.cn_bundle.crt

檔案目錄放在    /home/ubuntu/ybb/www.yangbb.cn/Nginx/  目錄下(自己選擇存放目錄)

3.修改nginx配置檔案

配置檔案路徑   /etc/nginx/nginx.conf

修改檔案

sudo vim /etc/nginx/nginx.conf

 修改service部分

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	# server_tokens off;

	 server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##
        server{
               listen 80;
               server_name yangbb.cn;
               return 301 https://$host$request_uri;

               }   
	server {
                
        	listen 443 ;
        	server_name yangbb.cn; #濉啟緇戝畾璇佷功鐨勫煙鍚?	        ssl on;
	        ssl_certificate /home/ubuntu/ybb/www.yangbb.cn/Nginx/1_www.yangbb.cn_bundle.crt;
	        ssl_certificate_key /home/ubuntu/ybb/www.yangbb.cn/Nginx/2_www.yangbb.cn.key;
        	ssl_session_timeout 5m;
	        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #鎸夌収榪欎釜鍗忚閰嶇疆
        	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#鎸夌収榪欎釜濂椾歡閰嶇疆
	        ssl_prefer_server_ciphers on;
        	location / {
                           	
        	root   /var/www/html; #絝欑偣鐩綍
            	index  index.html index.htm;
        	}
                error_page 497  https://$host$request_uri;
        }


	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;
	gzip_disable "msie6";

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
# 
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

修改示例

修改完按Esc進入控制模式,

再:w儲存。:wq是儲存並退出……

檢視當前nginx程序:

ps -aux | grep nginx

4.重啟nginx

sudo /etc/init.d/nginx restart

5.網頁檔案放在/var/www/html下