记录一下在centos7.x下面自定义编译安装nginx高性能的HTTP和反向代理web服务器
首先确认系统是否已安装nginx,不管有没有先用指令查一下
[root@myw ~]# whereis nginx
nginx:[root@myw ~]#
[root@myw ~]# netstat -lnp|grep nginx
[root@myw ~]#
[root@myw ~]# netstat -lnp|grep nginx
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1823/nginx: master
[root@myw ~]#
自定义安装先从官网下载nginx(nginx-1.20.2.tar.gz)上传(ftp)到centos的目录下(根据自己习惯选定目录或者按照要求选定目录)
/usr/local/
[root@myw ~]# cd /usr/local
[root@myw local]# ls
nginx-1.20.2.tar.gz
[root@myw local]#
解压
[root@myw local]# tar -xvzf nginx-1.20.2.tar.gz
安装之前
我个人喜欢将编译安装的nginx放在(/home)下面,先在/home下面创建nginx-black文件夹(重点)
[root@myw ~]# cd /home
[root@myw home]# ls
[root@myw home]# mkdir nginx-demo
[root@myw home]# ls
nginx-demo
[root@myw home]#
安装的时候可能出现一些错误,实际上需要提前安装好常用的包
yum -y install net-tools
yum install -y gcc gcc-c++ autoconf automake
yum -y install zlib-devel openssl openssl-devel pcre-devel
进入目录
[root@myw local]# cd nginx-1.20.2
[root@myw nginx-1.20.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@myw nginx-1.20.2]#
执行指令
./configure --prefix=/home/nginx-demo --with-stream --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
详情(configure显示的,我这里显示出来,方便确认状态)
[root@myw nginx-1.20.2]# ./configure --prefix=/home/nginx-demo --with-stream --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
checking for OS
+ Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
............................省略
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/home/nginx-demo"
nginx binary file: "/home/nginx-demo/sbin/nginx"
nginx modules path: "/home/nginx-demo/modules"
nginx configuration prefix: "/home/nginx-demo/conf"
nginx configuration file: "/home/nginx-demo/conf/nginx.conf"
nginx pid file: "/home/nginx-demo/logs/nginx.pid"
nginx error log file: "/home/nginx-demo/logs/error.log"
nginx http access log file: "/home/nginx-demo/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@myw nginx-1.20.2]#
编译安装
make && make install
详情(编译安装显示的,我这里显示出来,方便确认状态)
[root@myw nginx-1.20.2]# make && make install
make -f objs/Makefile
make[1]: Entering directory `/usr/local/nginx-1.20.2'
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I ..............省略
src/stream/ngx_stream_upstream_zone_module.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/ngx_modules.o \
objs/ngx_modules.c
cc -o objs/nginx \
objs/src/core/nginx.o \
objs/src/core/ngx_log.o \
objs/src/core/ngx_palloc.o \
objs/src/core/ngx_array.o \
objs/src/core/ngx_list.o \
objs/src/core/ngx_hash.o \
objs/src/core/ngx_buf.o \
objs/src/core/ngx_queue.o \
objs/src/core/ngx_output_chain.o \
objs/src/core/ngx_string.o \
objs/src/core/ngx_parse.o \
objs/src/core/ngx_parse_time.o \
objs/src/core/ngx_inet.o \
objs/src/core/ngx_file.o \
objs/src/core/ngx_crc32.o \
objs/src/core/ngx_murmurhash.o \
objs/src/core/ngx_md5.o \
objs/src/core/ngx_sha1.o \
objs/src/core/ngx_rbtree.o \
objs/src/core/ngx_radix_tree.o \
objs/src/core/ngx_slab.o \
objs/src/core/ngx_times.o \
objs/src/core/ngx_shmtx.o \
objs/src/core/ngx_connection.o \
objs/src/core/ngx_cycle.o \
objs/src/core/ngx_spinlock.o \
objs/src/core/ngx_rwlock.o \
objs/src/core/ngx_cpuinfo.o \
objs/src/core/ngx_conf_file.o \
objs/src/core/ngx_module.o \
objs/src/core/ngx_resolver.o \
objs/src/core/ngx_open_file_cache.o \
objs/src/core/ngx_crypt.o \
objs/src/core/ngx_proxy_protocol.o \
objs/src/core/ngx_syslog.o \
objs/src/event/ngx_event.o \
objs/src/event/ngx_event_timer.o \
objs/src/event/ngx_event_posted.o \
objs/src/event/ngx_event_accept.o \
objs/src/event/ngx_event_udp.o \
objs/src/event/ngx_event_connect.o \
objs/src/event/ngx_event_pipe.o \
objs/src/os/unix/ngx_time.o \
objs/src/os/unix/ngx_errno.o \
objs/src/os/unix/ngx_alloc.o \
objs/src/os/unix/ngx_files.o \
objs/src/os/unix/ngx_socket.o \
objs/src/os/unix/ngx_recv.o \
objs/src/os/unix/ngx_readv_chain.o \
objs/src/os/unix/ngx_udp_recv.o \
objs/src/os/unix/ngx_send.o \
objs/src/os/unix/ngx_writev_chain.o \
objs/src/os/unix/ngx_udp_send.o \
objs/src/os/unix/ngx_udp_sendmsg_chain.o \
objs/src/os/unix/ngx_channel.o \
objs/src/os/unix/ngx_shmem.o \
objs/src/os/unix/ngx_process.o \
objs/src/os/unix/ngx_daemon.o \
objs/src/os/unix/ngx_setaffinity.o \
objs/src/os/unix/ngx_setproctitle.o \
objs/src/os/unix/ngx_posix_init.o \
objs/src/os/unix/ngx_user.o \
objs/src/os/unix/ngx_dlopen.o \
objs/src/os/unix/ngx_process_cycle.o \
objs/src/os/unix/ngx_linux_init.o \
objs/src/event/modules/ngx_epoll_module.o \
objs/src/os/unix/ngx_linux_sendfile_chain.o \
objs/src/os/unix/ngx_linux_aio_read.o \
objs/src/event/ngx_event_openssl.o \
objs/src/event/ngx_event_openssl_stapling.o \
objs/src/core/ngx_regex.o \
objs/src/http/ngx_http.o \
objs/src/http/ngx_http_core_module.o \
objs/src/http/ngx_http_special_response.o \
objs/src/http/ngx_http_request.o \
objs/src/http/ngx_http_parse.o \
objs/src/http/modules/ngx_http_log_module.o \
objs/src/http/ngx_http_request_body.o \
objs/src/http/ngx_http_variables.o \
objs/src/http/ngx_http_script.o \
objs/src/http/ngx_http_upstream.o \
objs/src/http/ngx_http_upstream_round_robin.o \
objs/src/http/ngx_http_file_cache.o \
objs/src/http/ngx_http_write_filter_module.o \
objs/src/http/ngx_http_header_filter_module.o \
objs/src/http/modules/ngx_http_chunked_filter_module.o \
objs/src/http/modules/ngx_http_range_filter_module.o \
objs/src/http/modules/ngx_http_gzip_filter_module.o \
objs/src/http/ngx_http_postpone_filter_module.o \
objs/src/http/modules/ngx_http_ssi_filter_module.o \
objs/src/http/modules/ngx_http_charset_filter_module.o \
objs/src/http/modules/ngx_http_userid_filter_module.o \
objs/src/http/modules/ngx_http_headers_filter_module.o \
objs/src/http/ngx_http_copy_filter_module.o \
objs/src/http/modules/ngx_http_not_modified_filter_module.o \
objs/src/http/modules/ngx_http_static_module.o \
objs/src/http/modules/ngx_http_autoindex_module.o \
objs/src/http/modules/ngx_http_index_module.o \
objs/src/http/modules/ngx_http_mirror_module.o \
objs/src/http/modules/ngx_http_try_files_module.o \
objs/src/http/modules/ngx_http_auth_basic_module.o \
objs/src/http/modules/ngx_http_access_module.o \
objs/src/http/modules/ngx_http_limit_conn_module.o \
objs/src/http/modules/ngx_http_limit_req_module.o \
objs/src/http/modules/ngx_http_realip_module.o \
objs/src/http/modules/ngx_http_geo_module.o \
objs/src/http/modules/ngx_http_map_module.o \
objs/src/http/modules/ngx_http_split_clients_module.o \
objs/src/http/modules/ngx_http_referer_module.o \
objs/src/http/modules/ngx_http_rewrite_module.o \
objs/src/http/modules/ngx_http_ssl_module.o \
objs/src/http/modules/ngx_http_proxy_module.o \
objs/src/http/modules/ngx_http_fastcgi_module.o \
objs/src/http/modules/ngx_http_uwsgi_module.o \
objs/src/http/modules/ngx_http_scgi_module.o \
objs/src/http/modules/ngx_http_memcached_module.o \
objs/src/http/modules/ngx_http_empty_gif_module.o \
objs/src/http/modules/ngx_http_browser_module.o \
objs/src/http/modules/ngx_http_upstream_hash_module.o \
objs/src/http/modules/ngx_http_upstream_ip_hash_module.o \
objs/src/http/modules/ngx_http_upstream_least_conn_module.o \
objs/src/http/modules/ngx_http_upstream_random_module.o \
objs/src/http/modules/ngx_http_upstream_keepalive_module.o \
objs/src/http/modules/ngx_http_upstream_zone_module.o \
objs/src/http/modules/ngx_http_stub_status_module.o \
objs/src/stream/ngx_stream.o \
objs/src/stream/ngx_stream_variables.o \
objs/src/stream/ngx_stream_script.o \
objs/src/stream/ngx_stream_handler.o \
objs/src/stream/ngx_stream_core_module.o \
objs/src/stream/ngx_stream_log_module.o \
objs/src/stream/ngx_stream_proxy_module.o \
objs/src/stream/ngx_stream_upstream.o \
objs/src/stream/ngx_stream_upstream_round_robin.o \
objs/src/stream/ngx_stream_write_filter_module.o \
objs/src/stream/ngx_stream_limit_conn_module.o \
objs/src/stream/ngx_stream_access_module.o \
objs/src/stream/ngx_stream_geo_module.o \
objs/src/stream/ngx_stream_map_module.o \
objs/src/stream/ngx_stream_split_clients_module.o \
objs/src/stream/ngx_stream_return_module.o \
objs/src/stream/ngx_stream_set_module.o \
objs/src/stream/ngx_stream_upstream_hash_module.o \
objs/src/stream/ngx_stream_upstream_least_conn_module.o \
objs/src/stream/ngx_stream_upstream_random_module.o \
objs/src/stream/ngx_stream_upstream_zone_module.o \
objs/ngx_modules.o \
-ldl -lpthread -lcrypt -lpcre -lssl -lcrypto -ldl -lpthread -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/home/nginx-demo|" \
-e "s|%%PID_PATH%%|/home/nginx-demo/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/home/nginx-demo/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/home/nginx-demo/logs/error.log|" \
< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/usr/local/nginx-1.20.2'
make -f objs/Makefile install
make[1]: Entering directory `/usr/local/nginx-1.20.2'
test -d '/home/nginx-demo' || mkdir -p '/home/nginx-demo'
test -d '/home/nginx-demo/sbin' \
|| mkdir -p '/home/nginx-demo/sbin'
test ! -f '/home/nginx-demo/sbin/nginx' \
|| mv '/home/nginx-demo/sbin/nginx' \
'/home/nginx-demo/sbin/nginx.old'
cp objs/nginx '/home/nginx-demo/sbin/nginx'
test -d '/home/nginx-demo/conf' \
|| mkdir -p '/home/nginx-demo/conf'
cp conf/koi-win '/home/nginx-demo/conf'
cp conf/koi-utf '/home/nginx-demo/conf'
cp conf/win-utf '/home/nginx-demo/conf'
test -f '/home/nginx-demo/conf/mime.types' \
|| cp conf/mime.types '/home/nginx-demo/conf'
cp conf/mime.types '/home/nginx-demo/conf/mime.types.default'
test -f '/home/nginx-demo/conf/fastcgi_params' \
|| cp conf/fastcgi_params '/home/nginx-demo/conf'
cp conf/fastcgi_params \
'/home/nginx-demo/conf/fastcgi_params.default'
test -f '/home/nginx-demo/conf/fastcgi.conf' \
|| cp conf/fastcgi.conf '/home/nginx-demo/conf'
cp conf/fastcgi.conf '/home/nginx-demo/conf/fastcgi.conf.default'
test -f '/home/nginx-demo/conf/uwsgi_params' \
|| cp conf/uwsgi_params '/home/nginx-demo/conf'
cp conf/uwsgi_params \
'/home/nginx-demo/conf/uwsgi_params.default'
test -f '/home/nginx-demo/conf/scgi_params' \
|| cp conf/scgi_params '/home/nginx-demo/conf'
cp conf/scgi_params \
'/home/nginx-demo/conf/scgi_params.default'
test -f '/home/nginx-demo/conf/nginx.conf' \
|| cp conf/nginx.conf '/home/nginx-demo/conf/nginx.conf'
cp conf/nginx.conf '/home/nginx-demo/conf/nginx.conf.default'
test -d '/home/nginx-demo/logs' \
|| mkdir -p '/home/nginx-demo/logs'
test -d '/home/nginx-demo/logs' \
|| mkdir -p '/home/nginx-demo/logs'
test -d '/home/nginx-demo/html' \
|| cp -R html '/home/nginx-demo'
test -d '/home/nginx-demo/logs' \
|| mkdir -p '/home/nginx-demo/logs'
make[1]: Leaving directory `/usr/local/nginx-1.20.2'
[root@myw nginx-1.20.2]#
进入到使用的目录 conf是我们的配置文件,html是放置web代码的位置,logs日志,sbin是启动的
[root@myw nginx-1.20.2]# cd /home/nginx-demo
[root@myw nginx-demo]# ls
conf html logs sbin
[root@myw nginx-demo]# cd sbin
[root@myw sbin]# ./nginx
[root@myw sbin]# netstat -lnp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24579/nginx: master
[root@myw sbin]# kill -9 24579
[root@myw sbin]# netstat -lnp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24580/nginx: worker
[root@myw sbin]# kill -9 24580
[root@myw sbin]#
常见nginx的操作,我这里是是在sbin目录下面,所以没有写路径
[root@myw sbin]# ./nginx
[root@myw sbin]# netstat -lnp|grep nginx
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 1534/nginx: master
[root@myw sbin]# ./nginx -s stop
[root@myw sbin]# netstat -lnp|grep nginx
[root@myw sbin]# ./nginx
[root@myw sbin]# netstat -lnp|grep nginx
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 1544/nginx: master
[root@myw sbin]# ./nginx -s quit
[root@myw sbin]# netstat -lnp|grep nginx
[root@myw sbin]# ./nginx
[root@myw sbin]# netstat -lnp|grep nginx
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 1554/nginx: master
[root@myw sbin]# ./nginx -s reload // 8081端口改成8082 执行后可以看到生效了
[root@myw sbin]# netstat -lnp|grep nginx
tcp 0 0 0.0.0.0:8082 0.0.0.0:* LISTEN 1554/nginx: master
[root@myw sbin]# ./nginx -s reopen
[root@myw sbin]#
./nginx 启动
./nginx -s stop 快速停止
./nginx -s quit 优雅停止
./nginx -s reload 重新加载配置文件(十分有用,改配置不重启的情况)
./nginx -s reopen 不常用但很重要(当日志文件过大很有用)
./nginx -s reopen这个操作有些特殊,当nginx运行的的日志过大,直接删除是一个暴力的办法(一般还是要备份的),但nginx在linux运行时,即使我们改了日志文件的名字或路径,似乎日志照样会写到该文件里,那么运行时改文件备份就不好用了,备份nginx的日志文件方法
1.停止nginx服务,把日志文件改名字或者直接移动到其他目录下面,再启动nginx,此时会重新生成默认的日志文件,备份成功(不需要备份日志也可以删除,把硬盘资源放开),最大的缺点是要停止服务,小服务不影响,若是稍微大一点的服务还是有些影响的。
2.使用./nginx -s reopen(运行目录下面)他的作用是:当nginx默认的日志文件没有时,会创建默认的nginx日志文件,那么产生的日志便会写入这个里面,那么我们把nginx默认的日志文件改变了名字后,执行这条指令就会重新生成日志文件。
将access.log error.log文件改名
执行./nginx -s reopen
可以看到重新生成了新的日志文件
这个操作极大的方便了nginx在运行状态下,热备份日志,并释放空间
配置开机启动 使用服务方式
[Unit]
Description=nginx-black web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/home/nginx-demo/sbin/nginx -c /home/nginx-demo/conf/nginx.conf
ExecReload=/home/nginx-demo/sbin/nginx -s reload
ExecStop=/home/nginx-demo/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
准备好的文件nginx-demo.service 放入/etc/systemd/system/里面后刷新加载
/usr/lib/systemd/system/
[root@myw ~]# systemctl daemon-reload
[root@myw ~]# systemctl start nginx-demo.service
[root@myw ~]# netstat -lnp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 25639/nginx: master
[root@myw ~]# systemctl stop nginx-demo.service
[root@myw ~]# netstat -lnp|grep nginx
[root@myw ~]# systemctl start nginx-demo.service
[root@myw ~]# netstat -lnp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 25711/nginx: master
[root@myw ~]# systemctl restart nginx-demo.service
[root@myw ~]# netstat -lnp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 25734/nginx: master
[root@myw ~]# systemctl enable nginx-demo.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx-demo.service to /usr/lib/systemd/system/nginx-demo.service.
[root@myw ~]#
// 重新加载
systemctl daemon-reload
// 启动
systemctl start nginx-demo.service
// 重启
systemctl restart nginx-demo.service
// 停止
systemctl stop nginx-demo.service
// 查看启动状态
systemctl status nginx-demo.service
// 加入开机启动
systemctl enable nginx-demo.service
// 取消开机启动
systemctl disable nginx-demo.service
常见使用 一个可下载的文件服务器配置(nginx.conf)配置后使用./nginx -s reload重启
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8082;
server_name localhost;
location / {
root /home/nginx-file; #指定哪个目录作为Http文件服务器的根目录,如果你这里写了file就是你的根目录,那么访问的时候file就不会出现在目录中
autoindex on; #设置允许列出整个目录
autoindex_exact_size off; #默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
autoindex_localtime on; #默认为off,显示的文件时间为GMT时间。改为on后,显示的文件时间为文件的服务器时间
charset utf-8; #编码
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
常见使用 配置ssl(阿里的免费ssl) https请求方式,也支持http,同时根据路径代理到不同端口(nginx.conf)配置后使用./nginx -s reload重启
#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;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~ /api/ {
proxy_pass http://127.0.0.1:8062;
}
location ~ /web/ {
proxy_pass http://127.0.0.1:8061;
}
location / {
root html;
index index.html index.htm;
}
}
server {
listen 443 ssl;
server_name xxxx_xxxx.demo.com;
root html;
index index.html index.htm;
ssl_certificate cert/xxxx_xxxx.demo.com.pem;
ssl_certificate_key cert/xxxx_xxxx.demo.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
location ~ /api/ {
proxy_pass http://127.0.0.1:8062;
}
location ~ /web/ {
proxy_pass http://127.0.0.1:8061;
}
location / {
root html;
index index.html index.htm;
}
}
}
在nginx.conf目录下新建一个cert文件夹,把.pem和.key的文件放进去(可能需要对改文件授权,看系统是不是允许读取和使用)