博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

编译nginx并支持ssl

Posted on 2023-04-21 02:55  火冰·瓶  阅读(194)  评论(0)    收藏  举报

1.安装依赖包

apt update
apt-get install gcc
apt-get install libpcre3 libpcre3-dev
apt-get install zlib1g zlib1g-dev
apt-get install openssl
apt-get install libssl-dev

apt-get install gcc   libpcre3   libpcre3-dev   zlib1g   zlib1g-dev   openssl   libssl-dev  -y

 

2.下载nginx源码并解压
mkdir work
cd work
wget http://nginx.org/download/nginx-1.22.1.tar.gz
tar zxvf nginx-1.22.1.tar.gz
cd nginx-1.22.1

 

3.编译并安装

#prefix后面的路径是编译后安装nginx的路径
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install

 

4.检查
cd /usr/local/nginx/sbin
./nginx -V
./nginx -t

 

5.设置开机自动启动 

crontab -e

输入:

@reboot /usr/local/nginx/sbin/nginx

然后 reboot 重启服务器

 

6.配置
打开配置文件nginx.conf,路径为:/usr/local/nginx/conf/nginx.conf

    server {
    listen 80;
    server_name t1.xxx.net;
    location / {
        proxy_pass http://localhost:50000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
		}
	}




    server{
		listen 80;
		server_name t2.aaaa.com;
		rewrite ^(.*)$ https://${server_name}$1 permanent; #设置http自动转发https
	}



    server {
    listen 443 ssl;
    server_name t2.aaaa.com;
    ssl_certificate      /root/ssl/xxx.pem;
    ssl_certificate_key  /root/ssl/xxx.key;

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

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
    location / {
        proxy_pass http://localhost:50001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
		}
	}

  

 

7.常用命令  

./nginx
./nginx -s reload
./nginx -s stop