nginx安装ssl模块

 查看nginx安装的模块

linux nginx设置端口 nginx 配置端口_linux nginx设置端口

停止nginx服务

./nginx -s stop

进入nginx解压目录,重新编译nginx

cd /usr/local/nginx/nginx-1.17.6

// --prefix指定nginx的安装目录
./configure --prefix=/usr/local/nginx/nginx-config --with-http_stub_status_module --with-http_ssl_module

 编译完成之后在当前目录继续执行make命令(Makefile文件的目录)

make

之后,将当前目录下的objs目录中的nginx程序,拷贝到nginx的安装目录/usr/local/nginx/nginx-config/sbin目录下

cp objs/nginx /usr/local/nginx/nginx-config/sbin/nginx

进入nginx安装目录,查看nginx安装的模块

cd /usr/local/nginx/nginx-config/sbin/

./nginx -V

linux nginx设置端口 nginx 配置端口_nginx_02


nginx配置https

证书申请(此处省略1w步)成功后,将证书下载到本地

linux nginx设置端口 nginx 配置端口_https_03

 将下载的证书解压,解压后得到 ***.pem和***.key 两个文件,将这两个文件上传到linux目录(nginx安装目录:/usr/local/nginx/nginx-config/conf/cert)

linux nginx设置端口 nginx 配置端口_linux nginx设置端口_04

配置nginx.conf文件,在http模块下添加server模块

server {
        listen            443 ssl;   #SSL协议访问端口号为443。此处如未添加ssl,可能会造成Nginx无法启动。
        server_name       api-*******.com;  #将localhost修改为您证书绑定的域名,例如:www.example.com。

        root html;
        index index.html index.htm;

        ssl_certificate     cert/4316713_api-******.com.pem;   #将domain name.pem替换成您证书的文件名。
        ssl_certificate_key cert/4316713_api-******.com.key;   #将domain name.key替换成您证书的密钥文件名。

        ssl_session_timeout 10m;

        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 / {
            root html;   #站点目录。使用代理的话这行替换成 proxy_pass http://127.0.0.1:8080;
            index index.html index.htm;
        }
    }
}

开放443端口(只开放安全组端口是不行的)

// centos7 命令
// 添加
firewall-cmd --zone=public --add-port=443/tcp --permanent

// 重新载入
firewall-cmd --reload

// 查看是否开放成功,成功返回yes
firewall-cmd --zone=public --query-port=443/tcp

开放安全组443端口

linux nginx设置端口 nginx 配置端口_nginx_05

重启nginx服务

cd /usr/local/nginx/nginx-config/sbin/

./nginx -s reload

 浏览器中使用https访问,看下配置效果

linux nginx设置端口 nginx 配置端口_nginx_06


下面出一个 https 访问前端,前端 https 访问后端

# 前端 https 访问配置
    server {
        listen              443 ssl;					# 浏览器要访问前端 nginx 要访问的端口,例如浏览器访问:https://aa.com:9000/login , 这里就写 9000
        server_name         aa.com;						# 当前服务器映射的域名

        root html;
        index index.html    index.html;

        ssl_certificate     cert/6680189_aa.com.pem;	# ssl证书的路径
        ssl_certificate_key cert/6680189_aa.com.key;	# ssl证书的路径

        ssl_session_timeout 10m;

        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 / {
            root /opt/jar/vue/;							# 前端文件存放在linux服务器的目录 
            index  index.html;
            try_files $uri $uri/ /index.html;			# 这个是解决页面一刷新就跳404
        }

        location /server_file {							# (这个可以删除)这个是配置文件上传的,例如你的文件访问地址是:https://aa.com:9000/server_file/a.jpg
            alias /opt/jar/server_file/;				# 文件存放在linux服务器中的目录,server_file 要和上一行保持一致,因为使用的是 alias用户
            index index.html;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        error_page 497 https://$host:$server_port$uri$is_args$args;

    }

    # HTTPS server
    #
    # 后端 https 访问配置
    server {
        listen              9777 ssl;					# 后端 nginx 代理的后端端口,例如后端通过https访问是这样的:https://aa.com:9777
        server_name         aa.com;						# 当前服务器映射的域名

        root html;
        index index.html    index.html;

        ssl_certificate     cert/6680189_aa.com.pem;
        ssl_certificate_key cert/6680189_aa.com.key;

        ssl_session_timeout 10m;

        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://127.0.0.1:9700;			# 后端项目的地址,比如你的java程序的端口是9700,可以通过 http://ip:9700访问,也可以通过nginx代理的 9777端口通过域名访问:https://aa.com:9777
            index index.html index.htm;
        }


    }

    # 访问 80 跳转到 443 配置
    server {
        listen              80;
        server_name         aa.com;
        return 301          https://$host$request_uri;

    }

nginx配置ws

# 配置 ws, 通过 ws://ip:9909/ws 访问
server {
    listen       9909;							# nginx代理的端口,webSocket 可以通过这样访问: ws://IP:9909/路径
    server_name  localhost;

    location / {
        proxy_pass   http://127.0.0.1:9099/;	# netty服务的端口 9099,本地通过这样访问: ws://127.0.0.1:9099/路径
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_connect_timeout 300s;
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
    }
}

端口后面的/ws,是你项目中的路径

linux nginx设置端口 nginx 配置端口_linux nginx设置端口_07


nginx配置wss

# 配置 wss 访问,wss://aa.com:9909/ws
server {
    listen              9909 ssl;
    server_name         aa.com;

    root html;
    index index.html    index.html;

    ssl_certificate     cert/6680189_aa.com.pem;
    ssl_certificate_key cert/6680189_aa.com.key;

    ssl_session_timeout 10m;

    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://127.0.0.1:9099/;        # netty服务的端口 9099,本地通过这样访问: ws://127.0.0.1:9099/路径
        proxy_http_version 1.1;
        proxy_set_header Host  $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Nginx-Proxy true;
        proxy_redirect off; 
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_connect_timeout 300s; 
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
    }

}