nginx配置https服务器

方法一

1、创建证书

#cd /usr/local/nginx/conf
#openssl genrsa -des3 -out server.key 1024
#openssl req -new -key server.key -out server.csr
#openssl rsa -in server.key -out server_nopwd.key
#openssl x509 -req -days 365 -in server.csr -signkey server_nopwd.key -out server.crt
*******************************完成以上步骤,证书生成完成*************************************  
2、配置Nginx
  server {
   listen 80;
   server_name www.域名.com;
   rewrite ^(.*) https://$server_name$1 permanent;
 } 
     设置80和443走443
     检测自定义配置文件的完整路径是否,并重新加载 
     nginx -t
3、启动Nginx
cd /usr/local/nginx/sbin
./nginx
4、访问页面(tomcat部署的应用)
https服务器搭建,但如何让浏览器信任自己颁发的证书呢?
只要将之前生成的server.crt文件导入到系统的证书管理器就行了,具体方法:
控制面板 -> Internet选项 -> 内容 -> 发行者 -> 受信任的根证书颁发机构 -> 导入 -》选择server.crt
**********************************Nginx重启关闭命令*************************
nginx -s reload  :修改配置后重新加载生效
nginx -s reopen  :重新打开日志文件
nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确
 
关闭nginx:
nginx -s stop  :快速停止nginx
         quit  :完整有序的停止nginx

其他的停止nginx 方式:
ps -ef | grep nginx
kill -QUIT 主进程号     :从容停止Nginx
kill -TERM 主进程号     :快速停止Nginx
pkill -9 nginx          :强制停止Nginx
 
启动nginx:
nginx -c /path/to/nginx.conf

平滑重启nginx:
kill -HUP 主进程号
**********************************Nginx重启关闭命令*************************
*****************************************************Nginx配置文件*****************************************************************
#user  nobody;
worker_processes  1;
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  debug;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
http {
    include       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  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
    #server {
    #    listen       80;
    #    server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
 
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
    #    error_page   500 502 503 504  /50x.html;
    #    location = /50x.html {
    #        root   html;
    #    }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #location ~ /.ht {
        #    deny  all;
        #}
    #}
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
    # HTTPS server
    server {
        listen       443 ssl;
        server_name  30.0.0.3;
        ssl           on;
      # ssl_certificate      /usr/local/nginx/conf/server.crt;
         ssl_certificate      /usr/local/keys/newKeys/192.196.1.131.crt;
      # ssl_certificate_key  /usr/local/nginx/conf/server_nopwd.key;
      # /usr/local/keys
        ssl_certificate_key  /usr/local/keys/newKeys/192.196.1.131.key;
        ssl_protocols        SSLv2 SSLv3 TLSv1;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
 
        location / {
        proxy_pass http://30.0.0.3:8090/Sohu/index.jsp;
           # root   html;
           # index  index.html index.htm;
        }
    }
    server{
       listen    8080;
       server_name  30.0.0.3;
       rewrite ^(.*)$  https://$host$1 permanent;
    } 
}
方法二
执行脚本文件
#!/bin/sh
# create self-signed server certificate:
 
read -p "Enter your domain [www.example.com]: " DOMAIN
echo "Create server key..."
openssl genrsa -des3 -out $DOMAIN.key 1024
echo "Create server certificate signing request..."
SUBJECT="/C=US/ST=Mars/L=iTranswarp/O=iTranswarp/OU=iTranswarp/CN=$DOMAIN"
openssl req -new -subj $SUBJECT -key $DOMAIN.key -out $DOMAIN.csr
echo "Remove password..."
 
mv $DOMAIN.key $DOMAIN.origin.key
openssl rsa -in $DOMAIN.origin.key -out $DOMAIN.key
 
echo "Sign SSL certificate..."
openssl x509 -req -days 3650 -in $DOMAIN.csr -signkey $DOMAIN.key -out $DOMAIN.crt
 
echo "TODO:"
echo "Copy $DOMAIN.crt to /etc/nginx/ssl/$DOMAIN.crt"
echo "Copy $DOMAIN.key to /etc/nginx/ssl/$DOMAIN.key"
echo "Add configuration in nginx:"
echo "server {"
echo "    ..."
echo "    listen 443 ssl;"
echo "    ssl_certificate     /etc/nginx/ssl/$DOMAIN.crt;"
echo "    ssl_certificate_key /etc/nginx/ssl/$DOMAIN.key;"
echo "}"

 浏览器设置:

重启浏览器重新访问,一切ok
原文地址:https://www.cnblogs.com/java0619/p/6890657.html