nginx源码安装

nginx的官网下载

到nginx的官方下载站点http://nginx.org/en/download.html下载想要安装的nginx版本

1
wget http://nginx.org/download/nginx-1.16.1.tar.gz

安装ngninx

解压下载文件

1
tar zxvf nginx-1.16.1.tar.gz

隐藏server信息(可选)

1
2
3
4
5
6
7
8
9
vi src/http/ngx_http_header_filter_module.c ## 49-50行

内容:
static char ngx_http_server_string[] = "Server: nginx" CRLF;
static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;

更改为:
static char ngx_http_server_string[] = "Server: web-server" CRLF;
static char ngx_http_server_full_string[] = "Server:web-server " CRLF;

当然如果只是隐藏nginx的版本号可以在nginx.conf配置

1
server_tokens off

进入解压目录执行编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
cd nginx-1.16.1

#编译
./configure --prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--modules-path=/etc/nginx/ \
--sbin-path=/usr/sbin/nginx \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_image_filter_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_stub_status_module \
--with-debug \
--with-http_realip_module \
--with-stream \
--with-stream_ssl_preread_module

#安装
make
make install

配置优化nginx.conf

默认的nginx.conf文件很简单,许多参数需要自己配置,下面附上一个工作中长用的配置文件

bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
user  www  www;
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65536;
pid/var/run/nginx.pid;
events
{
useepoll;
worker_connections65536;
}
http
{
includemime.types;
default_typeapplication/octet-stream;
charset utf-8;

server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;

open_file_cache max=65536 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;

sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
server_tokens off;
port_in_redirect off;

#fastcgi
fastcgi_connect_timeout300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

#open gzip
gzip on;
gzip_vary on;
gzip_min_length1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level2;
gzip_typestext/plainapplication/x-javascript text/css application/xml;
gzip_disable "MSIE \[1-6\]\\.(?!.*SV1)";

#Proxy
proxy_connect_timeout600;
proxy_read_timeout600;
proxy_send_timeout600;
proxy_buffer_size16k;
proxy_buffers432k;
proxy_busy_buffers_size64k;
proxy_temp_file_write_size64k;

#Log format
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
server
{
listen 80 default;
server_name _;
return 403;
}
include vhosts/*.conf;
}