Nginx 在工作中已经有好几个环境在使用了,每次都是重新去网上找博客,各种编译配置,今天自己也整理一份安装文档和 nginx.conf 配置选项的说明,留作以后参考。
Linux 安装 nginx 1. 安装依赖库 1 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2. 安装正则表达式库 1 2 3 4 5 6 7 8 9 10 11 #下载 pcre 安装包到当前目录 wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.44/pcre-8.44.tar.gz #解压缩 tar -xvf pcre-8.42.tar.gz #进入目录 cd pcre-8.44 #安装编译 ./configure make && make install #查看 pcre 版本 pcre-config --version
3. 安装nginx 1 2 3 4 5 6 7 8 9 10 11 #下载 nginx 安装包到当前目录 wget https://nginx.org/download/nginx-1.16.1.tar.gz #解压缩 tar -xvf nginx-1.16.1.tar.gz #进入目录 cd nginx-1.16.1 #编译安装 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre/pcre-8.44 make && make install #查看版本 /usr/local/nginx/sbin/nginx -v
4. 配置 1 2 3 4 #创建用户组 /usr/sbin/groupadd nginx #在用户组创建用户 /usr/sbin/useradd -g nignx nignx
1 2 3 4 5 6 7 8 9 10 11 12 13 cd /usr/local/nginx #创建 conf.d 文件夹(存放配置) mkdir conf.d #创建 cert 文件夹(存放ssl证书) mkdir cert cd /usr/local/nginx/conf #配置 nginx.conf vim nginx.conf #打开 user 注解, 用户改为 nginx #在 server 上添加引入 conf 文件 include /usr/local/nginx/conf/conf.d/*.conf;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 cd conf.d vim test.conf #添加配置 server { listen 80; server_name chengzime.com.cn; rewrite ^(.*)$ https://$host$1 permanent; } server { listen 443 ssl; # ssl on; 报错时把 ssl 写在 443 后面 server_name chengzime.com.cn; ssl_certificate /usr/local/epod/nginx/conf/cert/4546333_chengzime.com.cn.pem; ssl_certificate_key /usr/local/epod/nginx/conf/cert/4546333_chengzime.com.cn.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #按照这个套件配置 ssl_prefer_server_ciphers on; location / { root /usr/local/chengzi/text; index index.html index.htm; } location ^~ /api { proxy_pass http://localhost:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; #proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Port $server_port; } }
5. 启动 1 2 3 4 5 6 7 8 9 10 11 12 13 14 cd /usr/local/nginx/sbin/ #验证脚本是否正确 ./nginx -t #表示正确 [root@iZ sbin]# ./nginx -t nginx: the configuration file /usr/local/epod/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/epod/nginx/conf/nginx.conf test is successful #运行nginx ./nginx #重新加载配置 ./nginx -s reload #停止nginx ./nginx -s stop