Nginx
来源:互联网 发布:解方程组软件 编辑:程序博客网 时间:2024/06/03 16:45
Nginx
sf2gis@163.com
2015年12月2日
1 目标:Http服务器的前端代理,实现请求转发功能。具有平衡负载和反射代理功能。
2 原理:作为Web服务器接受HTTP请求,然后根据配置跳转到其它的URL,根据配置处理返回结果。
3 流程:安装Nginx,配置HTTP服务器的地址、端口,启动服务,测试。
3.1 安装Nginx:下载stable版nginx的rpm预编译包,安装。
参考:http://jingyan.baidu.com/article/f71d603761c76d1ab641d1f9.html
sudo yum install nginx -y
测试:安装完成后使用whereis可以看到安装的路径:
启动路径:/usr/sbin/nginx
配置、日志等:/etc/nginx
本地发布路径:/usr/share/nginx
3.2 配置HTTP服务器的地址、端口:/etc/nginx/config.d/*.conf
nginx启动时会默认加载/etc/nginx/目录中的nginx.conf文件。默认的nginx.conf会加载/etc/nginx/conf.d/*.conf(conf.d目录中的所有conf文件)。
创建test.conf文件,其中指定目录和具体的跳转URL。
//test.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
#本地目录,/usr/share/nginx
location / {
root /usr/share/nginx/html;
index index2.html index.htm;
}
#指定/bd跳转到百度
location /bd {
root /root;
index index2.html index.htm;
proxy_passhttps://www.baidu.com/index.html;
}
#error_page 404 /404.html;
# redirect server error pages to the staticpage /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listeningon 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI serverlistening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, ifApache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
3.3 启动服务:sudoservice nginx restart
安装完成后默认是启动的(默认开机自动启动),配置完成后需要重启加载新的配置文件。
3.4 测试:访问nginx:localhost/bd
Nginx本地目录会跳转到配置文件中配置的百度地址(为什么会返回错误页面?)。
4 方法:
4.1 Nginx配置文件
4.1.1语法
文件架构:可以文件的任何地方使用include 文件名加载一个单独的文件作为此文件的一部分。
include/etc/nginx/mime.types;
以;结束语句。
注释#。
4.1.2组织结构:{}限定的树形结构。
参考:http://www.linuxidc.com/Linux/2013-11/92594.htm
根Main:用于全局设置
-|事件Events:
-|HTTP服务器相关HTTP:
--|主机Server:
---|URL匹配Location:
--|负载upstream:
4.1.3全局设置Main:设置Nginx程序相关参数
用户user:nginx的用户名(默认是nginx)。
进程数worker_processes:默认是1,一般设置为CPU数目。
全局日志error_log:格式为error_log地址 级别(debug,info,notice,warn,error,crit)。
进程ID存储文件pid:格式为pid 地址。
错误页面error_page:格式:error_page错误号 页面地址。
示例:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
error_page 500 502 503 504 /50x.html;
4.1.4HTTP服务器配置HTTP:http服务器处理参数
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
4.1.4.1 mime类型tpye:定义服务器处理的文件类型和扩展名。
type{
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
}
一般使用include引入文件列表。
include /etc/nginx/mime.types;
默认的MIME类型default_type:默认处理类型为二进制流。
default_type application/octet-stream;
4.1.4.2 log格式log_format:输出的日志条目格式。格式为log_format 格式名 具体的格式。
log_format main '$remote_addr - $remote_user[$time_local] "$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
4.1.4.3 访问日志access_log:格式:access_log 文件地址 格式名。
access_log /var/log/nginx/access.log main;
192.168.41.1 - -[02/Dec/2015:16:49:57 +0800] "GET / HTTP/1.1" - "-"
192.168.41.1 - -[02/Dec/2015:16:49:58 +0800] "GET /favicon.ico HTTP/1.1" -"-"
4.1.4.4 性能设置:gzip,tcpnodelay等。
sendfile on; 用于开启高效文件传输模式。
tcp_nopush on;防止TCP阻塞。
keepalive_timeout 0; 设置客户端连接保持活动的超时时间,超时服务器会关闭该连接。
gzip on; 压缩传输,提高传输速度。
4.1.5虚拟主机Server:域名、端口等
域名server_name:默认是localhost,可以有多个,空格分开。
端口listen:默认是80。
编码charset:默认是utf-8。
访问日志access_log:参见访问日志access_log:格式:access_log 文件地址 格式名。
地址匹配location:指定访问的url路径的跳转路径。参见:地址匹配location:跳转设置
本地根目录:root。
主页:index
4.1.6 地址匹配location:跳转设置
目标:指定匹配地址,如果匹配则跳转到配置中的URL。
原理:使用RE进行匹配,成功后改写URL进行跳转。
格式:location 匹配RE {跳转配置}
方法:跳转设置
本地根目录root:默认是文件目录。
主页index:可以有多个(用空格分开),顺序查找。
跳转URL地址proxy_pass:格式:proxy_pass URL。
其它可选设置:
proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;#以下是一些反向代理的配置,可选。
proxy_set_headerHost $host;
client_max_body_size10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 432k; #proxy_buffers缓冲区,网页平均在32k以下的设置
proxy_busy_buffers_size64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size64k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传
4.1.7事件events
事件模型use:格式 use epoll。Linux使用epoll性能好。
单进程最大连接数worker-connections:总连接数=进程数*单进程连接数。
参考:http://www.linuxidc.com/Linux/2013-03/81738.htm
示例:
events {
use epoll;
worker_connections 65535;
}
4.1.8负载均衡upstream:设置集群与权重
目标:定义一个虚拟主机作为服务器,实际使用一组集群作为实际的服务器,可以分配实际服务器的权重。
原理:从一个虚拟URL进行跳转,根据权重跳转到集群中不同的主机。
格式:upstream 虚拟URL{
server 主机名 weight=权重;
server 主机名 weight=权重;
…
}
使用:直接在URL中使用虚拟URL。
示例:
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的几率越大
server 192.168.8.1x:3128 weight=5;#本机上的Squid开启3128端口
server 192.168.8.2x:80 weight=1;
server 192.168.8.3x:80 weight=6;
}
server {
listen 80;
server_name 192.168.8.1;
#对aspx后缀的进行负载均衡请求
location ~.*\.aspx$ {
root /root; #定义服务器的默认网站根目录位置
index index.phpindex.html index.htm; #定义首页索引文件的名称
proxy_pass http://mysvr ;#请求转向mysvr定义的服务器列表
}
参考:http://www.cnblogs.com/xiaogangqq123/archive/2011/03/02/1969006.html
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
4.2 Nginx版本
稳定版:stable。
开发版:mainline。
历史版:Legacy。
参考:http://www.myexception.cn/ai/1773387.html
4.3 Nginx编译
参见:http://www.cnblogs.com/skynet/p/4146083.html
4.4 压力测试
参见:压力测试工具-WebBench.docx
5 示例
worker_processes 2;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" - "$request_body"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
proxy_cache_path /var/www/cache/static levels=1:2 keys_zone=static-cache:100m max_size=200m inactive=10m;
proxy_cache_path /var/www/cache/common levels=1:2 keys_zone=common-cache:100m max_size=600m inactive=10m;
proxy_temp_path /var/www/cache/tmp;
upstream fzjhq1
{
ip_hash;
server 192.168.1.131:8080;
}
upstream fzjhq2
{
server 192.168.1.131:8080;
}
server {
listen 80;
server_name 192.168.1.131;
charset utf-8;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
index index.html index.htm index.jsp;
proxy_pass http://192.168.1.131:8080/example_company3;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /example_company2 {
index index.html index.htm index.jsp;
proxy_pass http://192.168.1.131:8080/example_company2;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /cms {
proxy_pass http://192.168.1.131:8080/cms-ws;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /ws/sendmail {
root /jsp/;
index index.html index.htm;
proxy_pass http://fzjhq1/sendmail-ws/sendmail;
proxy_redirect off;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /datatransform-ws {
root /jsp/;
index index.html index.htm;
proxy_pass http://192.168.1.131:8080/datatransform-ws;
proxy_redirect off;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /sendmail/ws/0.1 {
root /jsp/;
index index.html index.htm;
proxy_pass http://fzjhq1/sendmail-ws;
proxy_redirect off;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /client-ws {
index index.jsp;
proxy_pass http://192.168.1.131:8080/client-ws;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /billing-ws {
index index.jsp;
proxy_pass http://192.168.1.131:8080/billing-ws;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /product-ws {
index index.jsp;
proxy_pass http://192.168.1.131:8080/product-ws;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /analysis-ws {
index index.jsp;
proxy_pass http://192.168.1.131:8080/analysis-ws;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /loctrace-ws {
index index.jsp;
proxy_pass http://192.168.1.131:8080/loctrace-ws;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /localert-ws {
index index.jsp;
proxy_pass http://192.168.1.131:8080/localert-ws;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location ~*(^/resetpwd/(.*)$){
proxy_pass http://192.168.1.131/example_company3/code-redirect.jsp?code=$2&page=resetPassword.jsp;
}
location /example_company3-oc {
index index.jsp;
proxy_pass http://192.168.1.131:8080/example_company3-oc;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /example_company3 {
index index.jsp;
proxy_pass http://192.168.1.131:8080/example_company3;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /busimeta-ws {
index index.jsp;
proxy_pass http://192.168.1.131:8080/busimeta-ws;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /example_company3-ws {
index index.jsp;
proxy_pass http://192.168.1.131:8080/example_company3-ws;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /euc {
index index.jsp;
proxy_pass http://192.168.1.131:8080/euc;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /euc-webapp {
index index.jsp;
proxy_pass http://192.168.1.131:8080/euc-webapp;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /uc {
index index.jsp;
proxy_pass http://192.168.1.131:8080/uc-ws;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /activiti-example_company3-webapp {
index index.jsp;
proxy_pass http://192.168.1.131:8080/activiti-example_company3-webapp;
proxy_redirect off;
client_max_body_size 65m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location ~*(^/news/(.*)/(.*)$){
proxy_pass http://192.168.1.131/news-info.jsp?_id=$3&type=$2;
}
location ~*(^/job/(.*)/(.*)$){
proxy_pass http://192.168.1.131/job-info.jsp?_id=$3&type=$2;
}
location ~*(^/download/(.*).apk$) {
proxy_pass http://192.168.1.131:8080/vmc-ws/ws/0.1/apk/download/$2.apk;
}
location /vmc {
proxy_pass http://192.168.1.131:8080/vmc-ws;
proxy_redirect off;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location ~*(^/(images|js|css)/(.*)$) {
proxy_pass http://fzjhq2/example_company/$2/$3;
proxy_cache static-cache;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;
}
location ~*(^/ltwmap/(.*)$) {
proxy_pass http://192.168.1.131:8080/loctrace-ws/feature/loctrace/map-pic-show.jsp?key=$2;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- nginx
- Nginx
- Nginx
- Nginx
- Nginx
- nginx
- nginx
- nginx
- nginx
- nginx
- nginx
- nginx
- nginx
- Nginx
- nginx
- nginx
- nginx
- nginx
- 去掉linux光标和制作linux Logo
- 文件断点续传和下载
- MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决方法
- 一片不错的V4L2
- Kinect for Windows SDK开发学习相关资源
- Nginx
- 第三讲:React Native & HTML5+(学习笔记1)
- C++11: unordered_map
- Xib学习总结
- apache二级域名绑定到目录
- 使用密钥ssh登录到主机
- Ping值和带宽之间关系
- Windows memcached 安装 window7 64位(亲试)
- 概率统计的基本知识