linux安装nginx及https化

Linux安装nginx

  • 安装
  1. 操作系统:centos
  2. 安装前先确保系统安装了g++、gcc、openssl-devel、pcre-devel和zlib-devel软件
yum install gcc-c++
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
  1. 上传nginx安装包nginx-1.13.0.tar.gz至linux指定位置(/usr/local/sowftware)
  2. 解压、编译、安装
cd /usr/local/sowftware
#解压
tar -zxvf nginx-1.13.0.tar.gz

#进入解压后的目录
cd nginx-1.13.0

#执行安装并指定安装位置
./configure --prefix=/usr/local/nginx --with-http_ssl_module
make
make install

至此nginx安装成功。

  • https化
    1.进入nginx安装目录,在该目录下新建cert目录,将nginx证书放入/usr/local/nginx目录下
    nginx目录结构
    cert目录下的证书
    在这里插入图片描述
  1. 进入nginx安装目录conf,修改nginx.conf文件
cd /usr/local/nginx/conf
vim nginx.conf

修改后的nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;
	
	server {
        listen       80;
        server_name  localhost;
		client_max_body_size 1000M;

        location / {
			proxy_pass	http://127.0.0.1:8080;
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
	
	# HTTPS server
    server {
		listen       443 ssl;
		server_name  localhost;
		client_max_body_size 1000M;

        ssl_certificate      ../cert/xxxxx.com.pem; #你的证书
        ssl_certificate_key  ../cert/xxxx.com.key;  #你的key

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
			add_header Content-Security-Policy upgrade-insecure-requests;
            proxy_pass http://127.0.0.1:8080; #映射到本地的Tomcat8080端口
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect http:// https://;
        }
    }
}

至此,在浏览器地址栏输入 http://域名/项目名,或 https://域名/项目名。均可访问到部署在tomcat(端口为8080)下的项目。

  • nginx命令
#启动
cd /usr/local/nginx/sbin/
./nginx

#停止
ps -ef|grep nginx
kill -9 主进程号

#重启
cd /usr/local/nginx/sbin/
./nginx -s reload

./nginx -h #帮助
./nginx -v #显示版本
./nginx -V #显示版本和配置信息
./nginx -t #测试配置
./nginx -q #测试配置时,只输出错误信息
./nginx -s stop #停止服务器
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值