Centos7源码编译安装Nginx
1.查看系统版本
cat /etc/redhat-release
wget http://nginx.org/download/nginx-1.24.0.tar.gz
3.选定安装目录,以data为例解压源码
tar xzvf nginx-1.24.0.tar.gz
4.配置安装信息
cd nginx-1.24.0
./configure --prefix=/data/nginx --with-http_stub_status_module --with-http_realip_module --without-http_gzip_module
- –prefix 表示指定安装目录为
/data/nginx
- –with-http_stub_status_module 表示启用stub_status 模块,用于查看nginx状态,接口地址
curl localhost:80/nginx_status
- –with-http_realip_module 表示启用 realip 模块,用于记录客户端的真是IP
- –without-http_gzip_module 表示禁用gzip模块,一般可不加,因为我本次安装报
./configure: error: the HTTP gzip module requires the zlib library
,而我又没有权限install the zlib library into the system
所以禁用了。他的作用是在发送响应时启用 gzip 压缩,以减小响应体的大小,提高传输速度和节省带宽。
5.编译源码并安装到步骤四指定的目录
make
make install
6.启动nginx
/data/nginx/sbin/nginx
7.修改配置,重启
vim /data/nginx/conf/nginx.conf
kill -s TERM `cat /data/nginx/logs/nginx.pid`
/data/nginx/sbin/nginx
一些常用配置
- 允许跨域
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if ($request_method = 'OPTIONS') {
return 204;
}
}
- 开启websocket
在编译Nginx时,需要加上–with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-http_gzip_static_module --with-http_dav_module --with-http_random_index_module --with-http_secure_link_module --with-http_auth_request_module --with-http_addition_module --with-http_geoip_module --with-http_image_filter_module --with-http_xslt_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-google_perftools_module --with-debug --with-http_flv_module --with-http_mp4_module --with-http_flv_module --with-cc-opt=‘-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2’ --with-ld-opt=‘-Wl,-z,relro -Wl,–as-needed -Wl,-z,now -Wl,–gc-sections’ --add-module=/path/to/ngx_http_websocket_module,其中/path/to/ngx_http_websocket_module是WebSocket模块的路径。
location /ws {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
- 开启健康检查
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}