Centos7源码安装Tengine
tengine是淘宝基于nginx开发的一款web服务,完全兼容nginx,并增加了部分功能,以下是在centos7.3最小化安装的情况下安装tengine-2.2.0版本,为了方便后期使用,我是安装了所有模块,如果明确所需的模块,可以自定义指定的模块
下载源码文件
到以下网站下载源码包:http://tengine.taobao.org/download_cn.html 或直接用wget命令下载
wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz解压下载文件
tar -zxf tengine-2.2.0.tar.gz安装部分模块依赖的软件
yum install -y gcc gcc-c++ pcre pcre-devel openssl-devel libxml2-devel libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed GeoIP-devel配置并指定安装目录;
为了方便后期使用,我是安装了所有模块,如果明确所需的模块,可以 ./configure --help 查看并自定义指定的模块
./configure --enable-mods-static=all --prefix=/opt/nginx --with-ipv6
make && make install查看安装模块
/opt/nginx/sbin/nginx -V服务控制,第15节有添加至系统服务的操作说明
/opt/nginx/sbing/nginx
/opt/nginx/sbing/nginx -s stop
/opt/nginx/sbing/nginx -s reloadproxy_pass配置说明
nginx proxy_pass后的url加不加/的区别,在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。下面四种情况分别用http://192.168.1.4/proxy/test.html 进行访问。 第一种:
location /proxy/ {
proxy_pass http://127.0.0.1:81/;
}会被代理到http://127.0.0.1:81/test.html 这个url
第二种(相对于第一种,最后少一个 /)
location /proxy/ {
proxy_pass http://127.0.0.1:81;
}会被代理到http://127.0.0.1:81/proxy/test.html 这个url
第三种:
location /proxy/ {
proxy_pass http://127.0.0.1:81/ftlynx/;
}会被代理到http://127.0.0.1:81/ftlynx/test.html 这个url。
第四种情况(相对于第三种,最后少一个 / ):
location /proxy/ {
proxy_pass http://127.0.0.1:81/ftlynx;
}会被代理到http://127.0.0.1:81/ftlynxtest.html 这个url
上面的结果都是本人结合日志文件测试过的。从结果可以看出,应该说分为两种情况才正确。即http://127.0.0.1:81 (上面的第二种) 这种和 http://127.0.0.1:81/…. (上面的第1,3,4种) 这种。
配置示例1
user nobody;
worker_processes 24;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 60000;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr $upstream_addr $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $upstream_response_time $upstream_status $connection';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
gzip on;
gzip_proxied any;
gzip_min_length 1k;
gzip_buffers 8 256k;
gzip_comp_level 3;
gzip_disable "MSIE [1-6]\."; #IE 1-6禁用gzip压缩
gzip_types text/css text/xml application/x-javascript application/octet-stream application/javascript image/png image/jpg application/json;
gzip_vary on;
client_max_body_size 1000m;
client_body_buffer_size 128k;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $host; #防止反代.js .css文件加载时失效,还是访问代理前路径。添加后即可解决.js .css文件访问时使用代理后的地址
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_path /opt/nginx/proxy_cache levels=1:2 keys_zone=cache_one:1000m inactive=1d max_size=30g;
proxy_temp_path /opt/nginx/proxy_temp;
proxy_ignore_client_abort on;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
upstream app_server {
session_sticky;
server 192.168.12.50:8082;
server 192.168.12.50:8083;
server 192.168.12.50:8084;
server 192.168.12.50:8085;
}
upstream yz_server {
server 192.168.12.50:8081;
server 192.168.12.60:8081 backup;
}
server {
listen 10443 ssl;
server_name app-tst.domain.com;
ssl_certificate /opt/nginx/conf/nginx-ssl/_.domain.com_bundle.crt;
ssl_certificate_key /opt/nginx/conf/nginx-ssl/_.domain.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
ssl_prefer_server_ciphers on;
charset utf-8;
location / {
proxy_pass http://app_server;
if ( $request_uri ~* /app/services\?_t=2017(.*)$ ) {
proxy_pass http://yz_server;
}
access_log logs/access-app-server.log main;
}
location ~ (gif|jpg|jpeg|png|bmp|swf|ico|rar|SIS|css|js|zip|java|jar|txt|flv|exe|wav|wma|mp3|xml|gz) {
proxy_pass http://app_server;
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
proxy_cache cache_one;
proxy_cache_valid 200 301 302 304 1d;
proxy_cache_valid 404 7d;
proxy_cache_valid any 1d;
proxy_cache_use_stale http_502 http_504 error timeout invalid_header;
proxy_cache_key $host:$server_port$request_uri$is_args$args;
add_header X-Cache $upstream_cache_status;
expires 1d;
access_log logs/access-app-tst.log main;
}
location /nginx_status {
stub_status on;
access_log off;
}
}
}配置示例2
user root;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';
access_log logs/access.log json;
sendfile on;
#tcp_nopush on;
client_max_body_size 1000m;
keepalive_timeout 65;
gzip on;
# 图片上传负载
upstream up-tomcat {
session_sticky;
server 192.168.12.10:8085;
server 192.168.12.10:8084;
}
server {
listen 80;
server_name dev.domain.com;
location /upload {
proxy_pass http://up-tomcat;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect default;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_cache_revalidate on;
proxy_cache_min_uses 3;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
access_log logs/access-upload.log json;
}
location /download {
root html;
index index.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
charset utf-8,gbk;
access_log logs/access-download.log;
}
location /zabbix {
proxy_pass http://192.168.12.41/zabbix;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect default;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_cache_revalidate on;
proxy_cache_min_uses 3;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
access_log logs/access-zabbix.log json;
}
location /fxj/ {
root /var/www/html; #php-fpm基于docker运行,指定目录为docker中的目录,启动docker时用-v参数来指定本机挂载目录。
fastcgi_pass 192.168.12.43:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
access_log logs/access-fxj.log json;
}
location /nginx_status {
stub_status on;
access_log off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name doc.domain.com;
location / {
proxy_pass http://showdoc;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect default;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
proxy_cache_revalidate on;
proxy_cache_min_uses 3;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
access_log logs/access-showdoc.log json;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}Nginx配置访问用户名、密码
nginx可以配置用户名和密码来限制对某些站点进行访问,不过保存的密码并不是明文的,而是进行了crypt加密后的字符串。加密可以用htpasswd来实现。
yum install -y httpd-tools
htpasswd -c /opt/nginx/passwd user # -c指定密码文件路径,user表示密码对应的用户名称,可自定义
New password: # 输入密码
Re-type new password: # 重复密码如果后续还要添加用户,不能使用 -c参数,否则会将之前的内容删除,直接指定密码文件 加用户名即可
htpasswd /opt/nginx/passwd newuser修改nginx配置; 在server 或 location 下加入以下两行内容
auth_basic "Restricted"; //提示内容
auth_basic_user_file /opt/nginx/passwd; //密码文件路径注:加在server中,server下所有目录均需要密码;加在指定location,该location访问需要密码
配置为下载服务器
location /download/ {
root /download/file/path;
autoindex on; #开启索引功能
autoindex_exact_size off; # 关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; # 显示本机时间而非 GMT 时间
}12、nginx禁止IP、非指定域名访问 禁止未知域名解析
在nginx.conf中的所有server前增加一个_的server_name ,return到404或403。后面每个server块中必须设置server_name 指定域名,否则不能访问。
server {
listen 80 default_server;
server_name _;
return 444;
}空白页
location = /empty.gif {
empty_gif;
}日志格式,写在http{}里
log_format main '$remote_addr $upstream_addr $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" $upstream_response_time $upstream_status $connection';json格式
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"status":"$status"}';centos7添加nginx为系统服务,通过systemctl 控制
在系统服务目录创建nginx.service文件
vi /usr/lib/systemd/system/nginx.service[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/opt/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target服务控制
systemctl start nginx
systemctl enable nginx
systemctl restart nginx
systemctl stop nginx关闭favicon.ico 日志记录 禁止访问隐藏文件
把以下配置放到 server {} 块.
# 关闭favicon.ico不存在时记录日志
location = /favicon.ico {
log_not_found off;
access_log off;
}
# 禁止访问robots.txt
location = /robots.txt {
deny all;
log_not_found off;
access_log off;
}
# 不允许访问隐藏文件例如 .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}nginx指定文件路径有两种方式root和alias,指令的使用方法和作用域:
[root] 语法:root path 默认值:root html 配置段:http、server、location、if [alias] 语法:alias path 配置段:location
root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。 root的处理结果是:root路径+location路径 alias的处理结果是:使用alias路径替换location路径 alias是一个目录别名的定义,root则是最上层目录的定义。 还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的。。。而root则可有可无~~
root示例:
location /test/ {
root html;
index index.html;
}访问 http://domain.com/test/index.html 实际路径为 html/test/index.html
alias示例:
location /test/ {
alias html/;
index index.html;
}访问 http://domain.com/test/index.html 实际路径为 html/index.html alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。而root则不会丢弃 location后面的路径,
注意:
使用alias时,目录名后面一定要加"/"。
alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
alias只能位于location块中。(root可以不放在location中)
缓存配置
location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
access_log off;
expires 30d;
}
location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
access_log off;
expires 24h;
}
location ~* ^.+\.(html|htm)$ {
expires 1h;
}301 跳转
location /path {
rewrite ^/path/(.*)$ https://domain.com/path/$1;
}
或
rewrite ^ http://example.com$request_uri? permanent;301 跳转(官方推荐)
server_name www.example.com;
return 301 http://www.abc.com$request_uri;
server {
server_name example.com;
# [...]
}同一个服务,内部 rewrite跳转
location /image {
rewrite ^/image/(.*)$ /folderName/image/$1 last;
}BAD:
rewrite ^/(.*)$ http://example.com/$1 permanent;GOOD:
rewrite ^ http://example.com$request_uri? permanent;BETTER:
return 301 http://example.com$request_uri;nginx屏蔽指定URL恶意扫描
^0481a6
将日志中被恶意尝试扫描的URL直接返回444状态码,该状态码表明服务器没有返回信息给客户端并且关闭了连接
location ~ ^/(wp|think-php|phpmyadmin)/.*\.php$ {
access_log off;
log_not_found off;
return 444;
}
# 使用gzip炸弹
location ~ ^/(wp|think-php|phpmyadmin)/.*\.php$ {
access_log off;
log_not_found off;
return 301 http://example.com/10G.gzip;
}nginx location匹配规则
location匹配命令
~ #波浪线表示执行一个正则匹配,区分大小写
~* #表示执行一个正则匹配,不区分大小写
^~ #^~表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
= #进行普通字符精确匹配
@ # "@" 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
=前缀的指令严格匹配这个查询。如果找到,停止搜索。
所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。
正则表达式,在配置文件中定义的顺序。
如果第3条规则产生匹配的话,结果被使用。否则,如同从第2条规则被使用。location 匹配的优先级(与location在配置文件中的顺序无关) = 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。 普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。 ^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。 最后匹配理带有""和"*"的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。
ngxin 反代websocket协议示例
WebSocket 可以减小客户端与服务器端建立连接的次数,减小系统资源开销,只需要一次 HTTP 握手,整个通讯过程是建立在一次连接/状态中,也就避免了 HTTP 的非状态性,服务端会一直与客户端保持连接,直到你关闭请求,同时由原本的客户端主动询问,转换为服务器有信息的时候推送
大多数用它还来做实时通信的功能,我们可以使用 Swoole 的 WebSocket\Server 来作为服务端
客户端的话,支持就很多了,比如 Chrome/Firefox/高版本 IE/Safari 等浏览器内置了 JS 语言的 WebSocket 客户端、微信小程序开发框架内置的 WebSocket 客户端、异步的 PHP 程序中可以使用 Swoole\Http\Client 作为 WebSocket 客户端等等
ws 和 wss 是什么?有什么区别 Websocket 使用 ws 或 wss 的统一资源标志符,类似于 HTTP 或 HTTPS,其中 wss 表示在 TLS 之上的 Websocket ,相当于 HTTPS 了
默认情况下,Websocket 的 ws 协议使用 80 端口,wss 协议默认使用 443 端口
location /ws/ {
proxy_pass http://1.1.1.1:8000/;
proxy_http_version 1.1; # 代理http协议的版本,因为这里是代理websocket协议,所以http协议使用1.1 用于keepalive连接 也就是保持长连接(这一步必须有)
proxy_set_header Upgrade "websocket"; # 表示要“升级”成websocket协议(这一步必须有)
# proxy_set_header Upgrade $http_upgrade; 这个和上面的proxy_set_header Upgrade "websocket";是一个意思,两个写其中一个即可
proxy_set_header Connection "Upgrade"; # 表示要求协议“升级”,也就是说这不是一个普通的http协议
}重启后 访问时使用 ws://domain.com:port/ws/ 即可
开启压缩提高 数据传输效率
通过开启gzip压缩功能将较大的数据进行压缩后再传输,提高内容加载速度。可配置位置http、server和location
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 3;
gzip_types text/plain application/javascript text/css application/xml application/json;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
同时还要根据实际情况调整gzip_types,来控制要压缩的类型。 支持的类型如下
text/plain application/javascript text/css application/xml application/json image/png image/jpg 最后更新于