centos安装Nginx

年爸 1年前 ⋅ 518 阅读

1.下载Nginx

http://nginx.org/en/download.html

2.安装依赖

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

3.解压文件至安装目录

tar -xzvf nginx-1.24.0.tar.gz -C /usr/local

4.编译Nginx

./configure --prefix=/usr/local/nginx --with-http_ssl_module
make && make install

./configure 对即将安装的软件进行配置,检查当前环境是否满足安装软件的依赖关系。

--prefix  设置安装目录;

--with-http_ssl_module 设置在Nginx中允许使用http_ssl_module模块。

5.添加系统服务

vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
 
[Install]
WantedBy=default.target


chmod 755 /usr/lib/systemd/system/nginx.service

6.常用命令

启动: systemctl start nginx
停止: systemctl stop nginx
重启: systemctl restart nginx
重新加载配置文件: systemctl reload nginx
查看nginx状态: systemctl status nginx
开机启动: systemctl enable nginx

7.常用代理配置

# 硬盘上的html路径
location /html {
    alias                /var/data/xxx/html/xxx/; # 指定目录所在路径
    index                index.html index.htm;    # 页面索引
    autoindex            on;                      # 开启目录浏览
    autoindex_format     html;                    # 以html风格将目录展示在浏览器中
    autoindex_exact_size off;                     # 切换为 off 后,以可读的方式显示文件大小,单位为 KB、MB 或者 GB
    autoindex_localtime  on;                      # 以服务器的文件时间作为显示的时间
}
# 端口代理
location /gateway/ {	   
    proxy_pass http://127.0.0.1:8080/;
}
# 负载均衡
location /server/ {
    proxy_pass http://server-cluster/;
}
# 负责均衡-代理配置
upstream server-cluster {
    server ip:port;
    server ip:port;
}
# HTTPS server
server {
    listen                    443 ssl;
    server_name               xxx.com;

    ssl_certificate           /usr/local/nginx/cert/xxx.crt;
    ssl_certificate_key       /usr/local/nginx/cert/xxx.key;

    ssl_session_timeout       5m;
    ssl_protocols             TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers               ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
	
    # html根目录
    location / {
        root   html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
}

 

 


全部评论: 0

    我有话说: