上一篇 <<<前后端分離場景通過Nginx調(diào)試本地后端接口
下一篇 >>>OSI七層模型與層上協(xié)議
Nginx配置
參考地址:http://tengine.taobao.org/nginx_docs/cn/docs/
涉及模塊:
- ngx_http_core_module
- ngx_http_limit_conn_module
- ngx_http_proxy_module
- ngx_http_ssl_module
- ngx_http_upstream_module
- ngx_http_rewrite_module
- ngx_http_referer_module
- ngx_http_headers_module
- ngx_http_log_module
- nginx_cookie_flag_module
1、控制單IP并發(fā)連接數(shù)
編輯配置文件nginx.conf酝润,在添加如下內(nèi)容:
limit_conn_zone $binary_remote_addr zone=addr:10m;
server
{
listen 80;
server_name www.baidu.com;
index index.html index.htm index.jsp;
root /usr/local/www;
#Zone limit;
location / {
limit_conn addr 3;
limit_rate 20k;
}
}
2、禁止目錄瀏覽
編輯配置文件nginx.conf,在添加如下內(nèi)容:
autoindex off;
3、限制目錄執(zhí)行權(quán)限(PHP)
編輯配置文件nginx.conf汞扎,在添加如下內(nèi)容:
location ~ /(attachments|upload)/.*\.(php|php5)?$ {
deny all;
}
4冤今、錯(cuò)誤頁面重定向
編輯配置文件nginx.conf,在添加如下內(nèi)容:
proxy_intercept_errors on;
error_page 404 /404.html;
location = /404.html {
root /usr/local/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
}
5犀暑、隱藏Nginx版本信息
編輯配置文件nginx.conf,在添加如下內(nèi)容:
server_tokens off;
6烁兰、限制HTTP請求方法
編輯配置文件nginx.conf耐亏,在添加如下內(nèi)容:
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 404;
}
7、Nginx用戶降權(quán)
編輯配置文件nginx.conf沪斟,在添加如下內(nèi)容:
user nginx www;
8广辰、自定義Nginx緩存
編輯配置文件nginx.conf,在添加如下內(nèi)容:
proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小
proxy_buffers 4 32k; #proxy_buffers緩沖區(qū)主之,網(wǎng)頁平均在32k以下的設(shè)置
proxy_busy_buffers_size 64k; #高負(fù)荷下緩沖大性竦酢(proxy_buffers*2)
proxy_temp_file_write_size 64k; #設(shè)定緩存文件夾大小,大于這個(gè)值槽奕,將從upstream服務(wù)器傳
9几睛、過濾非法的UA
編輯配置文件nginx.conf,在添加如下內(nèi)容:
if ($http_user_agent ~* "java|python|perl|ruby|curl|bash|echo|uname|base64|decode|md5sum|select|concat|httprequest|httpclient|nmap|scan" ) {
return 403;
}
10粤攒、過濾不支持的URL
編輯配置文件nginx.conf所森,在添加如下內(nèi)容:
location ~* \.(bak|save|sh|sql|mdb|svn|git|old)$ {
rewrite ^/(.*)$ $host permanent;
}
11、強(qiáng)制域名訪問
編輯配置文件nginx.conf夯接,在添加如下內(nèi)容:
if ( $host !~* 'taobao.com' ) {
return 403;
}
12焕济、去掉無用的Nginx模塊
在編譯安裝時(shí),執(zhí)行./configure方法時(shí)加上以下配置指令盔几,可以顯式的刪除不用的模塊:
./configure --without-http_dav_module --withouthttp_spdy_module
13晴弃、SSL配置
需要購買證書,否則瀏覽器將有風(fēng)險(xiǎn)提示
- 開啟HTTPS服務(wù)
ssl_certificate /etc/nginx/sites-enabled/certs/tecmintlovesnginx.crt;
ssl_certificate_key /etc/nginx/sites-enabled/certs/tecmintlovesnginx.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
- 禁止SSL 打開TLS
需要購買證書,否則瀏覽器將有風(fēng)險(xiǎn)提示
ssl_protocols TLSv1.2;
- Cookie 設(shè)置 Secure
在location中設(shè)置
# 只支持 proxy 模式下設(shè)置上鞠,SameSite 不需要可刪除际邻,如果想更安全可以把 SameSite 設(shè)置為 Strict
proxy_cookie_path / "/; httponly; secure; SameSite=Lax";
- 重定向到HTTPS
需要購買證書,否則瀏覽器將有風(fēng)險(xiǎn)提示
return 301 https://$server_name$request_uri;
防火墻配置
強(qiáng)烈建議在網(wǎng)絡(luò)防火墻或WAF中統(tǒng)一配置規(guī)則
1旗国、開啟SELinux
2枯怖、拒絕 icmp協(xié)議
禁止ping
iptables -A OUTPUT -p icmp --icmp-type echo-request -j DROP
iptables -A INPUT -p icmp --icmp-type echo-request -j DROP
3、限制SSH連接IP
iptables -A INPUT -p tcp --dport 22 -s 10.14.0.0/16 -j ACCEPT /*允許22端口數(shù)據(jù)進(jìn)入*/
4能曾、關(guān)閉不需要的端口
默認(rèn)關(guān)閉所有input端口,只保留必要服務(wù)的端口
iptables -P INPUT DROP /*丟棄所有進(jìn)入機(jī)器的數(shù)據(jù)包*/
iptables -A INPUT -p tcp --dport 22 -s 10.14.0.0/16 -j ACCEPT /*允許22端口數(shù)據(jù)進(jìn)入*/
Nginx配置
1肿轨、下載最新版Tenginx寿冕、zlib
wget http://tengine.taobao.org/download/tengine-2.3.2.tar.gz
wget http://www.zlib.net/zlib-1.2.11.tar.gz
2、添加用戶和用戶組
groupadd -g 666 www
useradd -u 801 -g 666 -Mn -s /sbin/nologin nginx
3椒袍、編譯并安裝zlib
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install
4驼唱、編譯并安裝luajit
下載nginx專用版,否則有坑
yum -y install gcc
yum -y install gcc-c++
wget https://github.com/openresty/luajit2/archive/v2.1-20200102.tar.gz
tar -zxvf v2.1-20200102.tar.gz && cd luajit2-2.1-20200102
make && make install PREFIX=/usr/local/luajit
export LUAJIT_LIB=/usr/local/luajit/lib LUAJIT_INC=/usr/local/luajit/include/luajit-2.1
echo /usr/local/luajit/lib > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig
5驹暑、下載ngx_devel_kit和lua-nginx-module
wget https://github.com/loveshell/ngx_lua_waf/archive/master.zip
wget https://github.com/vision5/ngx_devel_kit/archive/v0.3.1.tar.gz
tar -xzvf v0.3.1.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.14.tar.gz
tar -xzf v0.10.14.tar.gz
6玫恳、編譯Tenginx并安裝
tar -zxvf tengine-2.3.2.tar.gz
cd tengine-2.3.2
./configure --prefix=/usr/local/nginx --pid-path=/usr/local/nginx/nginx.pid --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_ssl_module --user=nginx --group=www --add-module=/usr/local/nginx/modules/ngx_devel_kit-0.3.1 --add-module=/usr/local/nginx/modules/lua-nginx-module-0.10.14
make && make install
5、修改配置文件
- /usr/local/nginx/conf/nginx.conf
user nginx www;
worker_processes auto;
error_log /data/www/nginx/logs/error.log;
#error_log logs/error.log notice;
# error_log logs/error.log info;
#error_log "pipe:rollback logs/error_log interval=1d baknum=7 maxsize=2G";
pid /usr/local/nginx/nginx.pid;
events{
use epoll;
worker_connections 51200;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
server_names_hash_bucket_size 512;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
#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 logs/access.log main;
# access_log "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G" main;
add_header X-Content-Type-Options nosniff;
#防止跨站腳本 Cross-site scripting (XSS)
add_header X-XSS-Protection "1; mode=block";
add_header X-Frame-Options "SAMEORIGIN";
#add_header Content-Security-Policy: "default-src self";
add_header Content-Security-Policy "script-src * 'unsafe-inline' 'unsafe-eval'";
#add_header Content-Security-Policy "content *";
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 60;
tcp_nodelay on;
autoindex off;
#fastcgi_connect_timeout 300;
#fastcgi_send_timeout 300;
#fastcgi_read_timeout 300;
#fastcgi_buffer_size 64k;
#fastcgi_buffers 4 64k;
#fastcgi_busy_buffers_size 128k;
#fastcgi_temp_file_write_size 256k;
#fastcgi_intercept_errors on;
gzip on;
gzip_min_length 1k;
# 設(shè)置壓縮所需要的緩沖區(qū)大小
gzip_buffers 4 16k;
# 設(shè)置gzip壓縮針對的HTTP協(xié)議版本
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
# IE6以下瀏覽器不壓縮
gzip_disable "MSIE [1-6]\.";
error_page 403 /403.html;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name b2cfl.ispacechina.com;
root /usr/share/nginx/html;
client_max_body_size 20m;
ssl_certificate "/etc/nginx/cert/htsl_jks.pem";
ssl_certificate_key "/etc/nginx/cert/htsl_jks.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
#ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256::!MD5;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_protocols TLSv1.2;
#ssl_stapling on;
#ssl_stapling_verify on; # Requires nginx => 1.3.7
#ssl_prefer_server_ciphers on;
#ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
#ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0
#ssl_session_cache shared:SSL:1m;
#ssl_session_tickets off; # Requires nginx >= 1.5.9
#ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
add_header Strict-Transport-Security "max-age=80720000; preload";
add_header X-Content-Type-Options nosniff;
#防止跨站腳本 Cross-site scripting (XSS)
add_header X-XSS-Protection "1; mode=block";
add_header X-Frame-Options "SAMEORIGIN";
#add_header Content-Security-Policy: "default-src self";
#add_header Content-Security-Policy "content *";
add_header Content-Security-Policy "script-src * 'unsafe-inline' 'unsafe-eval'";
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
# location / {
# proxy_pass http://127.0.0.1:9001;
# proxy_http_version 1.1;
# proxy_redirect off;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection keep-alive;
# proxy_set_header Host $host;
# proxy_cache_bypass $http_upgrade;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-Proto $scheme;
# }
}
include /data/www/nginx/conf/*.conf;
}
- /data/www/nginx/conf/icasc_cloud_server_8080.conf
- /data/www/nginx/conf/icasc_cloud_vue_80.conf
配置iptables
- 禁止ping
iptables -A OUTPUT -p icmp --icmp-type echo-request -j DROP
iptables -A INPUT -p icmp --icmp-type echo-request -j DROP
推薦閱讀:
<<<DNS域名解析過程
<<<初識Nginx與負(fù)載均衡
<<<Nginx使用場景之靜態(tài)服務(wù)器
<<<Nginx使用場景之虛擬主機(jī)(動(dòng)靜分離)
<<<Nginx使用場景之反向代理
<<<Nginx使用場景之負(fù)載均衡和故障轉(zhuǎn)移(主從)
<<<Nginx使用場景之跨域解決
<<<Nginx使用場景之資源壓縮
<<<Nginx使用場景之?dāng)?shù)據(jù)緩存
<<<前后端分離場景通過Nginx調(diào)試本地后端接口
<<<Nginx Location指令詳解
<<<Nginx全局變量
<<<Nginx導(dǎo)致圖片下載失敗的原因分析
<<<Nginx+Consul+upSync實(shí)現(xiàn)動(dòng)態(tài)負(fù)載均衡
<<<Nginx+Lvs+keepAlived實(shí)現(xiàn)高可用
<<<Linux環(huán)境安裝Nginx
<<<Linux環(huán)境安裝keepAlived
<<<Keepalived虛擬vip功能