Linux系统 WEB服务Nginx

NGINX 编译安装

nginx安装涉及pcre、zlib库文件、openssl加密包安装

tar -zxvf pcre-8.38.tar.gz
cd pcre-8.38
./configure
make
make install


tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install


tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.38 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.0.1t
make
make install

一、https服务
启动nginx,查看nginx模块,发现已经添加
/usr/local/nginx/sbin/nginx -V 
HTTPS其实是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信息的模块。服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后的数据
https协议原理
首先,客户端与服务器建立连接,各自生成私钥和公钥,是不同的。服务器返给客户端一个公钥,然后客户端拿着这个公钥把要搜索的东西加密,称之为密文,并连并自己的公钥一起返回给服务器,服务器拿着自己的私钥解密密文,然后把响应到的数据用客户端的公钥加密,返回给客户端,客户端拿着自己的私钥解密密文,把数据呈现出来

二、证书和私钥的生成
注意:一般生成的目录,应该放在nginx/conf/ssl目录
1.创建服务器证书密钥文件 server.key:
openssl genrsa -des3 -out server.key 1024
输入密码,确认密码,自己随便定义,但是要记住,后面会用到。
2.创建服务器证书的申请文件 server.csr
openssl req -new -key server.key -out server.csr
输出内容为:
Enter pass phrase for root.key: ← 输入前面创建的密码 
Country Name (2 letter code) [AU]:CN ← 国家代号,中国输入CN 
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音 
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 
Organizational Unit Name (eg, section) []: ← 可以不输入 
Common Name (eg, YOUR name) []: ← 此时不输入 
Email Address []:admin@mycompany.com ← 电子邮箱,可随意填
Please enter the following ‘extra’ attributes 
to be sent with your certificate request 
A challenge password []: ← 可以不输入 
An optional company name []: ← 可以不输入
4.备份一份服务器密钥文件
cp server.key server.key.org
5.去除文件口令
openssl rsa -in server.key.org -out server.key
6.生成证书文件server.crt
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

三、配置文件
1.下面为配置文件 /usr/local/nginx/conf/vhost/daj.conf
server{
#比起默认的80 使用了443 默认 是ssl方式 多出default之后的ssl
listen 443 default ssl;
#default 可省略
#开启 如果把ssl on;这行去掉,ssl写在443端口后面。这样http和https的链接都可以用
ssl on;
#证书(公钥.发送到客户端的)
ssl_certificate ssl/server.crt;
#私钥,
ssl_certificate_key ssl/server.key;
#下面是绑定域名
server_name www.daj.com;
location / {
#禁止跳转
proxy_redirect off;
#代理淘宝
proxy_pass https://www.tao.com/; 

}


四、Nginx的配置文件nginx.conf配置详解如下
#定义Nginx运行的用户和用户组
#user nobody;
#user sp_ctpsp;
#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 4;
#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /opt/app/ctpsp/nginx1.6.2/logs/error.log info;
error_log /opt/app/ctpsp/nginx1.6.2/logs/error.log error;
error_log /opt/app/ctpsp/nginx1.6.2/logs/error.log notice;
#进程文件
pid /opt/app/ctpsp/nginx1.6.2/nginx.pid;
#nginx保持的最大文件打开数量,应于系统保持一致。(生产上没有配置)
worker_rlimit_nofile 65535;

#工作模式与连接数上限
events {
#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
use epoll;
multi_accept on;
#单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections 102400;
}

#设定http服务器
http {
include mime.types; #文件扩展名与文件类型映射表 
default_type application/octet-stream; #默认文件类型
#access_log logs/access.log main;
sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁
#定义nginx日志格式
#tcp_nopush on;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
keepalive_timeout 65; #长连接超时时间,单位是秒

ssi on; #开启ssi支持,默认是off
ssi_silent_errors on; #默认值是off,开启后在处理SSI文件出错时不输出错误提示:”[an error occurred while processing the directive] ”
ssi_types text/shtml; #默认是ssi_types text/html,所以如果需要htm和html支持,则不需要设置这句,如果需要shtml支持,则需要设置:ssi_types text/shtml
#gzip模块设置
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 2; #压缩等级
gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
gzip_vary on;

日志格式设置。
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从那个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。

access_log logs/host.access.log main;
access_log logs/host.access.404.log log404;
用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径;

server_names_hash_bucket_size 128;
#保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。因此,如果Nginx给出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.

client_header_buffer_size 4k;
客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。

large_client_header_buffers 8 128k;
客户请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值,如果
header过大,它会使用large_client_header_buffers来读取。

open_file_cache max=102400 inactive=20s;
这个指令指定缓存是否启用。
例: open_file_cache max=1000 inactive=20s; 
open_file_cache_valid 30s; 
open_file_cache_min_uses 2; 
open_file_cache_errors on;

open_file_cache_errors
语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.
open_file_cache_min_uses
语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location 这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如果使用更大的值,文件描述符在cache中总是打开状态.
open_file_cache_valid
语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.


client_max_body_size 300m;
设定通过nginx上传文件的大小

sendfile on;
sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。

tcp_nopush on;
此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用

proxy_connect_timeout 90; 
后端服务器连接的超时时间_发起握手等候响应超时时间

proxy_read_timeout 180;
连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)

proxy_send_timeout 180;
后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据

proxy_buffer_size 256k;
设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头,默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小,不过可以将其设置为更小

proxy_buffers 4 256k;
设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认情况也为分页大小,根据操作系统的不同可能是4k或者8k

proxy_busy_buffers_size 256k;

proxy_temp_file_write_size 256k;
设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长

proxy_temp_path /data0/proxy_temp_dir;
proxy_temp_path和proxy_cache_path指定的路径必须在同一分区

proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#设置内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
keepalive_timeout 120;
keepalive超时时间。

tcp_nodelay on;

client_body_buffer_size 512k;
如果把它设置为比较大的数值,例如256k,那么,无论使用firefox还是IE浏览器,来提交任意小于256k的图片,都很正常。如果注释该指令,使用默认的client_body_buffer_size设置,也就是操作系统页面大小的两倍,8k或者16k,问题就出现了。
无论使用firefox4.0还是IE8.0,提交一个比较大,200k左右的图片,都返回500 Internal Server Error错误

proxy_intercept_errors on;
表示使nginx阻止HTTP应答代码为400或者更高的应答。


#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
upstream one.session.com { 
ip hash; #使用hash算法进行轮询负载
server 172.28.41.249:8080 weight=1 max_fails=2 fail_timeout=30s;
server 172.28.41.194:8080 weight=1 max_fails=2 fail_timeout=30s;

upstream two.session.com {
ip hash;
server 172.28.41.194:8844 weight=1 max_fails=2 fail_timeout=30s;
}
upstream three.session.com {
ip hash;
server 172.28.41.195:8028 weight=1 max_fails=2 fail_timeout=30s;
}

#proxy_cache_path /usr/local/nginx1.6.2/cache/webserver levels=1:2 keys_zone=webserver:20m max_size=1g;


server {
listen 8844; #监听端口
server_name 172.28.41.130
index index.shtml ;
#去除域名后缀
location / { 
root /opt/app/ctpsp/staticfile/source ; #定义静态资源文件存放的位置
index index.shtml ; #除去已index index.html index.shtml结尾的后缀文件


#本地动静分离反向代理配置1
#所有jsp的页面均交由tomcat或resin处理
location ~ \.(jsp|jspx|do)?$ {
proxy_pass http://one.session.com;
proxy_set_header Host $host; 
proxy_set_header X-Real-IP $remote_addr; #以下是一些反向代理的配置
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
proxy_cache_valid 200 10m; #参数被用于NGINX做反向代理时,其指定特定http状态码在nginx的cache内存活的时间长短
}
location /policylaw {
proxy_pass http://one.session.com/ctpsp_policylaw/; #代理内容需要tomcat有项目名,不能为根
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #以下是一些反向代理的配置 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP 
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时) 
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时) 
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时) 
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置 
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) 
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 
proxy_cache_valid 200 10m; #参数被用于NGINX做反向代理时,其指定特定http状态码在nginx的cache内存活的时间长短

location /hdwiki/ {
proxy_pass http://three.session.com/hdwiki/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #以下是一些反向代理的配置 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP 
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时) 
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时) 
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时) 
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置 
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) 
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 
proxy_cache_valid 200 10m; #参数被用于NGINX做反向代理时,其指定特定http状态码在nginx的cache内存活的时间长短
}


location ^~ /ctpsp_guaranteeservice/ {
proxy_pass http://two.session.com/ctpsp_guaranteeservice/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #以下是一些反向代理的配置 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP 
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时) 
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时) 
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时) 
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置 
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) 
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 
# proxy_cache webserver; #参数被用于NGINX做反向代理时,其指定特定http状态码在nginx的cache内存活的时间长短
proxy_cache_valid 200 10m;
}
location /gm {
proxy_pass http://two.session.com/ctpsp_guaranteeservice/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #以下是一些反向代理的配置 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP 
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时) 
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时) 
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时) 
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置 
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) 
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 
# proxy_cache webserver; #参数被用于NGINX做反向代理时,其指定特定http状态码在nginx的cache内存活的时间长短
proxy_cache_valid 200 10m;
}
#本地动静分离反向代理配置2
#所有静态文件由nginx直接读取不经过tomcat或resin
location ~ .*\.(shtml|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|docx|ico|ppt|pdf|xls|xlsx|mp3|wma)$ {
root /opt/app/ctpsp/staticfile/source;
}
location ~ .*\.(js|css)?$ {
root /opt/app/ctpsp/staticfile/source; #定义静态资源文件存放的位置
}

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;
}

}
#nginx监听请求重定向
server {
listen 8844;
server_name navi.cebpubservice.com;
rewrite "^/(.*)$" http://www.cebpubservice.com/tenderdocument/jsp/platformManage/platformManageDoor.jsp$1 break;
}

}

posted @ 2019-09-10 09:45  浮尽繁华  阅读(265)  评论(0编辑  收藏  举报