nginx 安装、配置 http + https 访问 tomcat 项目以及配置 http 强转 https

1、在 linux (CentOS)上安装 nginx

第一步:添加 nginx 存储库html

xshell> yum install epel-release

第二步:安装 nginxlinux

xshell> yum install nginx

使用 yum 安装 nginx 后,nginx 的默认路径为:/etc/nginxnginx


第三步:启动 nginxshell

xshell> systemctl start nginx
若是你正在运行防火墙,请运行如下命令以容许 HTTP 和 HTTPS 通讯:
xshell> firewall-cmd --permanent --zone=public --add-service=http 
xshell> firewall-cmd --permanent --zone=public --add-service=https
xshell> firewall-cmd --reload

第四步:访问 nginx 以验证安装是否成功浏览器

因为 nginx 默认的端口号为 80 ,直接在浏览器上输入你的 ip 地址 +80 端口号(例如:111.111.111.111:80),可以访问到 tomcat

nginx 的页面即表示 nginx 已经安装成功。相似于下图。服务器


第五步: 设置系统启动时候启用 nginx session

xshell> systemctl enable nginx

2、使用 nginx 配置域名访问 tomcat 项目

第一步: 打开 nginx 的配置文件 nginx.conf。.net

具体路径:/etc/nginx/nginx.conf3d

第二步:配置文件须要修改的源码(配置讲解在第三步)

upstream xx{ 
        server 193.112.53.11:8080;
    }
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  www.yyddd.cn;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
		proxy_pass http://xx/cz_manager/;
		proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

第三步:配置文件修改讲解


第四步:配置完成后保存,而后重启(从新加载) nginx ,否则访问不到

xshell> nginx -s reload

第五步:验证

在浏览器上,输入本身配置的域名,若是能访问到配置的项目,就说明配置成功。若是还有什么不懂的话能够给

我留言。

3、nginx 配置 https 

第一步:申请证书

关于证书的申请,请转步看我另一篇博客的第一部分:

http://www.javashuo.com/article/p-hpryrbzt-dz.html

第二步:把证书上传到服务器上

下面是我在腾讯云申请的 ssl 证书,下载证书后,打开的文件内容以下,咱们使用的是 nginx 配置 https ,全部

打开 Nginx 文件,把文件下的两个文件上传到服务器上(放在哪里本身决定,知道路径就行,后面配置须要用到

证书的路径)



第三步:打开配置文件 nginx.conf 

1.在配置文件中,找到下面这段代码,把注释给去掉,或者直接复制我下面这段代码进行修改也行。

server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        server_name  www.yydddd.cn;
        root         /usr/share/nginx/html;

        ssl_certificate "/etc/nginx/1_yydddd.cn_bundle.crt";
        ssl_certificate_key "/etc/nginx/2_yydddd.cn.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
		proxy_pass http://xx/cz_manager/;
		proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

2.配置解析


3.配置完成后保存,而后重启(从新加载) nginx ,否则访问不到

xshell> nginx -s reload

4.验证

直接在浏览器输入:https://www.yydddd.cn(请换成本身的域名)

可以正常访问到的话,就是配置 https 成功了。

4、nginx 强制使用 https 访问

所谓强制使用 https 访问,就是用户使用 http 访问的时候,直接转到 https 访问。

只须要修改 http 的配置便可:


重启nginx

xshell> nginx -s reload
使用域名(例如:www.yyddd.cn)直接访问的时候,直接跳转到 https 访问的话,就说明配置成功了。