centos 解压安装nginx,并配置
官网:http://nginx.org/en/download.html
一、安装依赖
首先要安装几个依赖环境
1.gcc
nginx是C语言开发,建议在linux上运行。
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc
FQS:./configure: error: C compiler cc is not found
[root@localhost ~]# yum install gcc-c++
2.PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
[root@localhost ~]# yum install -y pcre pcre-devel
3.zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
[root@localhost ~]# yum install -y zlib zlib-devel
4.openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
[root@localhost ~]# yum install -y openssl openssl-devel
二、下载解压
1、官网地址:http://nginx.org/en/download.html
2、上传解压
下载之后,用ssh工具上传到centos目录
进入到当前文件夹解压
[root@localhost nginx]# tar -zxvf nginx-1.18.0.tar.gz
这是我这里的目录,我这里已经解压完毕了
三、安装
命令详解:https://nginx.org/en/docs/configure.html
3.1 从源码构建 configure
cd进入解压nginx之后目录,也就是nginx-1.18.0
使用命令配置生成。 它定义了系统的各个方面,包括 nginx 方法 允许用于连接处理。 最后,它会创建一个 .configureMakefile
我这里的目录/mnt/common/nginx/nginx,以下参数酌情修改
[root@localhost nginx-1.18.0]#
./configure \
--prefix=/mnt/common/nginx/nginx \
--sbin-path=/mnt/common/nginx/nginx/sbin/nginx \
--conf-path=/mnt/common/nginx/nginx/conf/nginx.conf \
--pid-path=/mnt/common/nginx/nginx/nginx.pid \
--lock-path=/mnt/common/nginx/nginx/nginx.lock \
--error-log-path=/mnt/common/nginx/nginx/log/error.log \
--http-log-path=/mnt/common/nginx/nginx/access.log \
--http-client-body-temp-path=/mnt/common/nginx/nginx/temp/client_body_temp \
--http-proxy-temp-path=/mnt/common/nginx/nginx/temp/proxy_temp \
--http-fastcgi-temp-path=/mnt/common/nginx/nginx/temp/fastcgi_temp \
--http-uwsgi-temp-path=/mnt/common/nginx/nginx/temp/fastcgi_temp \
--http-scgi-temp-path=/mnt/common/nginx/nginx/temp/scgi_temp \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-threads \
--with-file-aio \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--without-http-cache
提前创建tmp目录,因为配置里面用到了,这个不会自动创建
sudo mkdir -p /mnt/common/nginx/nginx/temp
如果权限不够,提示bash: ./configure: 权限不够
-bash: ./configure: 权限不够
[root@localhost nginx-1.18.0]# chmod +x configure
3.2 编译并安装
(都是在解压之后的目录执行)
[root@centos8 nginx-1.18.0]# make && make install
3.3 启动
第三行启动不起来,是因为少了文件夹,第四行命令创建就可以了
[root@localhost sbin]# ./nginx
3.4 访问
http://ip:80/
nginx在线配置网站:https://nginx.p2hp.com/config/?global.app.lang=zhCN
四、基本配置
# 全局配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
# 连接数配置
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream getwayy{
#weight 权重每个是1 2次请求,分别访问到服务
server 127.0.0.1:8081 weight=1;
server 127.0.0.1:8081 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 访问80端口根目录,就会访问到这里
location / {
root ./html;
index index.html index.htm;
#代理的文件
proxy_pass http://getwayy
}
#带有admin的路径,就会访问到这
location /admin {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
五、配置成系统服务
把Nginx应用服务设置成为系统服务,方便对Nginx服务的启动和停止等相关操作,具体实现步骤:
1、在/usr/lib/systemd/system
目录下添加nginx.service,内容如下:
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
2、添加完成后如果权限有问题需要进行权限设置
chmod 755 /usr/lib/systemd/system/nginx.service
3、使用系统命令来操作Nginx服务
启动: systemctl start nginx
停止: systemctl stop nginx
重启: systemctl restart nginx
重新加载配置文件: systemctl reload nginx
查看nginx状态: systemctl status nginx
开机启动: systemctl enable nginx
部分配置解释
#user nobody;
# 工作进程数,一般与 CPU 核数等同
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
#每一个进程打开的最大连接数,包含了nginx与客户端和nginx与upstream之间的连接(受限于操作系统)
worker_connections 1024;
#可以一次建立多个连接
multi_accept on;
# Linux下多路复用IO接口select/poll的增强版本,它能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
#启用sendfile()系统调用来替换read()和write()调用,减少系统上下文切换从而提高性能,当 nginx 是静态文件服务器时,能极大提高nginx的性能表现,而当 nginx 是反向代理服务器时,则没什么用了
sendfile on;
#当tcp_nopush设置为on时,表示启用TCP_CORK选项,Nginx会等待缓冲区填满后再发送数据包,以减少网络传输的次数,提高效率。当tcp_nopush设置为off时,表示禁用TCP_CORK选项,Nginx会立即发送数据包,适用于实时性要求较高的场景。
tcp_nopush on;
#当tcp_nodelay设置为on时,表示启用TCP_NODELAY选项,Nginx会立即发送小数据包,适用于实时性要求较高的场景。当tcp_nodelay设置为off时,表示禁用TCP_NODELAY选项,Nginx会等待一定时间或者缓冲区填满后再发送数据包,以减少网络传输的次数,提高效率。
tcp_nodelay on;
#连接超时 时间定义 默认秒 默认65秒
keepalive_timeout 65;
gzip on; #开启gzip压缩功能
gzip_min_length 10k; #设置允许压缩的页面最小字节数; 这里表示如果文件小于10个字节,就不用压缩,因为没有意义,本来就很小.
gzip_buffers 4 16k; #设置压缩缓冲区大小,此处设置为4个16K内存作为压缩结果流缓存
gzip_http_version 1.1; #压缩版本
gzip_comp_level 6; #设置压缩比率,最小为1,处理速度快,传输速度慢;9为最大压缩比,处理速度慢,传输速度快; 这里表示压缩级别,可以是0到9中的任一个,级别越高,压缩就越小,节省了带宽资源,但同时也消耗CPU资源,所以一般折中为6
gzip_types text/css text/xml application/javascript; #制定压缩的类型,线上配置时尽可能配置多的压缩类型!
gzip_disable "MSIE [1-6]\."; #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
gzip_vary on; #选择支持vary header;改选项可以让前端的缓存服务器缓存经过gzip压缩的页面; 这个可以不写,表示在传送数据时,给客户端说明我使用了gzip压缩
upstream getwayy{
#weight 权重每个是1 2次请求,分别访问到服务
server 127.0.0.1:8081 weight=1;
server 127.0.0.1:8081 weight=1;
keepalive 300;
}
server {
listen 88;
listen [::]:88 ipv6only=on;
server_name 127.0.0.1;
location / {
#proxy_set_header 是指客户端的真实IP,如果设置了$remote_addr这个值,后端服务器就能获取到客户端的真实IP
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
add_header Access-Control-Allow-Origin *;
proxy_pass getwayy;
}
location /baidu {
proxy_pass https://www.baidu.com/;
}
location /nginx_status {
stub_status on;
access_log off;
}
}
}