Linux从入门到放弃 编译安装nginx

官网下载nginx.org

[root@7 nginx-1.16.1]# wget  -P  /app/tools   http://nginx.org/download/nginx-1.16.1.tar.gz
[root@7 nginx-1.16.1]# tar -xzvf nginx-1.16.1.tar.gz	# 源代码

修改nginx源码配置文件

nginx-1.16.1/src/core/nginx.h
nginx-1.16.1/src/http/ngx_http_header_filter_module.c
nginx-1.16.1/src/http/ngx_http_special_response.c
[root@7 nginx-1.16.1]# vim src/core/nginx.h
 13 #define NGINX_VERSION      "8.5.50"
 14 #define NGINX_VER          "Tomcat/" NGINX_VERSION
 22 #define NGINX_VAR          "Tomcat"
[root@7 nginx-1.16.1]# vim src/http/ngx_http_header_filter_module.c
 49 static u_char ngx_http_server_string[] = "Server: tomcat" CRLF;
[root@7 nginx-1.16.1]# vim src/http/ngx_http_special_response.c 
36:"<hr><center>Tomcat</center>" CRLF

安装依赖

[root@7 nginx-1.16.1]# yum install -y   openssl-devel  pcre-devel

进行编译前的配置

[root@7 nginx-1.16.1]# ./configure --prefix=/app/nginx-1.16.1  --user=nginx  --group=nginx --with-http_stub_status_module  --with-http_ssl_module
										#安装目录				# 用户			# 组
[root@7 nginx-1.16.1]# useradd -s /sbin/nologin -M nginx 

进行编译

[root@7 nginx-1.16.1]# make

进行安装

[root@7 nginx-1.16.1]# make install

检查

[root@7 nginx-1.16.1]# nginx-1.16.1/sbin/nginx -V
nginx version: Tomcat/8.5.50
built by gcc 8.2.1 20180905 (Red Hat 8.2.1-3) (GCC) 
built with OpenSSL 1.1.1c FIPS  28 May 2019
TLS SNI support enabled
configure arguments: --prefix=/app/nginx-1.16.1 --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module

管理nginx

[root@7 nginx-1.16.1]# ln -s /app/nginx-1.16.1/sbin/nginx /sbin/
[root@7 nginx-1.16.1]# nginx	# 启动nginx
[root@7 nginx-1.16.1]# nginx -t 	# 语法检查
[root@7 nginx-1.16.1]# nginx -s reload	# 重新读取配置文件  要求nginx在运行 
[root@7 nginx-1.16.1]# nginx -s stop	# 关闭 nginx
[root@7 nginx-1.16.1]# vim /app/nginx-1.16.1/conf/nginx.conf
# 在http中添加
server_tokens off;	# 隐藏版本号

[root@7 nginx-1.16.1]# nginx -s reload
[root@7 nginx-1.16.1]# curl -I 39.105.190.251
HTTP/1.1 200 OK
Server: Tomcat
Date: Fri, 14 Feb 2020 03:24:58 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 14 Feb 2020 02:29:27 GMT
Connection: keep-alive
ETag: "5e460607-264"
Accept-Ranges: bytes

修改用户

[root@7 nginx-1.16.1]# useradd -s /sbin/nologin -M wy
[root@7 nginx-1.16.1]# vim /app/nginx-1.16.1/conf/nginx.conf

user  wy wy;	# 添加

[root@7 nginx-1.16.1]# ps aux | grep nginx
root     19689  0.0  0.4  26464  3524 ?        Ss   10:48   0:00 nginx: master process nginx
wy       20303  0.0  0.5  59340  4468 ?        S    11:54   0:00 nginx: worker process
root     20314  0.0  0.1  12112   988 pts/0    S+   11:55   0:00 grep --color=auto nginx

上传文件限制

[root@7 nginx-1.16.1]# vim /app/nginx-1.16.1/conf/nginx.conf

client_max_body_size 100m;	# 添加到server里

用户从哪里跳转过来的(通过域名) referer 控制

if  ( $http_referer  !~  “www.baidu.com” ) {   #放行一些搜索网站   白名单
return  403;
}
# 添加在server里

Nginx站点目录及文件权限优化

整体	root root	file 644	dir 755
上传	nginx nginx file 644	dir 755
nginx php/tomcat 用户统一
[root@7 nginx-1.16.1]# find /app/nginx-1.16.1/html/ -type f | xargs chmod 644

[root@7 nginx-1.16.1]# find /app/nginx-1.16.1/html/ -type d | xargs chmod 755

[root@7 nginx-1.16.1]# chown -R www.www  /app/nginx/html/uploads

tsar监控nginx

[root@7 nginx-1.16.1]# vim conf/nginx.conf

	location /nginx_status {
		stub_status ;
		access_log off;
		allow 172.16.1.0/24;
		allow 127.0.0.1;
		deny all;
	}
[root@7 nginx-1.16.1]# vim /etc/tsar/tsar.conf 

 19 mod_nginx on
-i 刷新时间
-l 持续
-s 指定列
[root@7 nginx-1.16.1]# tsar --nginx -i 1 -l
Time              --------------------------------------------------------------nginx------------------------------------------------------------- 
Time              accept  handle    reqs  active    read   write    wait     qps      rt  sslqps  spdyps    sslf  sslv3f   h2qps  sslhds    sslk   
14/02/20-15:57:12   1.00    1.00    1.00    1.00    0.00    1.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   
14/02/20-15:57:13   1.00    1.00    1.00    1.00    0.00    1.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   

[root@7 nginx-1.16.1]# tsar --nginx -i 1 -l -s time,qps,active
Time              ------nginx----- 
Time              active     qps   
14/02/20-15:58:58   1.00    1.00   

cpu亲和力

[root@7 nginx-1.16.1]# vim conf/nginx.conf

worker_cpu_affinity auto;

nginx打开文件数量

[root@7 nginx-1.16.1]# vim conf/nginx.conf

worker_rlimit_nofile 65535;

gzip压缩

[root@7 nginx-1.16.1]# vim conf/nginx.conf
#添加server中
	gzip on;	# 开启
	gzip_min_length 1k;	# 大于1k的进行压缩
	gzip_buffers 4 16k;	# 设置压缩缓存
	gzip_comp_level 2;	# 压缩级别 数字越大 压缩率越小(占用空间) 占用CPU越高
	gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;	#压缩格式

最终配置

[root@7 ~]# vim /app/nginx/conf/nginx.conf

user  wy wy;	# 修改	用户
worker_processes  2;	# 修改	链接数
worker_cpu_affinity  auto ;  	# 添加	CPU
worker_rlimit_nofile 65535;	# 添加	文件数
events {
    worker_connections  10240;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;
    keepalive_timeout  65;
    server_tokens off;
    gzip on;	# 添加 压缩
    gzip_min_length 1k;    
    gzip_buffers 4 16k;    
    gzip_comp_level 2;    
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php  ;    
    server {
        listen       80;
        server_name  localhost;
        client_max_body_size 100m;	# 添加	最大上传速率
	location  ~*   \.(html|js|css|jpg|jpeg)$  {
	}
        location / {
            root   html;
            index  index.html index.htm;
        }
        location  /nginx_status {	# tsar 配置
        stub_status ;
        allow 172.16.1.0/24;
        allow 127.0.0.1;
        deny all;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值