Nginx名称版本
> vi scr/core/nginx.h
HTTPS协议
http {
server {
listen 80;
listen 443 ssl;
server_name localhost;
ssl_certificate /home/keys/server.crt;
ssl_certificate_key /home/keys/server.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.2;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
...
}
}
多端自动切换
http {
server {
location / {
root html/dist;
index index.html;
if ($http_user_agent ~* 'Mobile') {
root html/mobile;
index index.html;
}
}
}
}
反向代理
http {
server {
proxy_connect_timeout 5s;
proxy_send_timeout 10s;
proxy_read_timeout 1800s;
fastcgi_connect_timeout 5s;
fastcgi_send_timeout 10s;
fastcgi_read_timeout 1800s;
client_max_body_size 100m;
client_body_buffer_size 100m;
location /api/ {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /ws {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
}
stream {
server {
listen 3308;
proxy_pass 127.0.0.1:3306;
}
}
负载均衡
http {
upstream backend {
ip_hash;
least_conn;
server backend1 weight=1 max_fails=3 fail_timeout=30s;
server backend2 weight=2 max_fails=3 fail_timeout=30s;
server backend3 weight=3 max_fails=3 fail_timeout=30s;
}
server {
location / {
proxy_pass http://backend;
}
}
}
并发优化
worker_processes auto;
worker_cpu_affinity auto;
events {
use epoll;
worker_connections 10240;
multi_accept on;
accept_mutex on;
accept_mutex_delay 500ms;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
keepalive_requests 100000;
client_header_timeout 15s;
client_body_timeout 15s;
send_timeout 15s;
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
limit_conn perip 100;
limit_conn perserver 10000;
client_body_buffer_size 16k;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
output_buffers 4 32k;
postpone_output 1460;
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types
application/atom+xml
application/geo+json
application/javascript
application/x-javascript
application/json
application/ld+json
application/manifest+json
application/rdf+xml
application/rss+xml
application/xhtml+xml
application/xml
font/eot
font/otf
font/ttf
image/svg+xml
text/css
text/javascript
text/plain
text/xml;
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
open_file_cache_errors on;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_connect_timeout 5s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
location = /index.html {
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Pragma "no-cache";
add_header Expires "0";
expires off;
}
location ~* .(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
expires 1y;
gzip on;
gzip_types application/javascript text/css image/svg+xml;
add_header Access-Control-Allow-Origin *;
add_header Content-Security-Policy "require-sri-for script style";
}
location / {
try_files $uri $uri/ /index.html;
if ($request_filename !~* .html$) {
add_header Cache-Control "public, max-age=86400";
expires 1d;
}
}
}
安全拦截
http {
server {
deny all;
allow 127.0.0.1;
autoindex off;
server_tokens off;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; object-src 'none'";
add_header X-Permitted-Cross-Domain-Policies "master-only";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
add_header Referrer-Policy "no-referrer-when-downgrade";
add_header Expires "0";
add_header Cache-Control "no-cache, no-store, must-revalidate";
location / {
root html;
index index.html;
internal;
}
}
}