Nginx常用配置

重写index.php

1
2
3
4
5
6
7
8
9
server {
listen 80;
server_name domain;
root /var/www/myProject;

location / { # rewrite index.php
try_files $uri /index.php$request_uri;
}
}

启用https

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
server {
listen 80;
listen 443 ssl http2;
server_name domain;
root /var/www/myProject;

# http jump https
if ($scheme = http) {
return 301 https://$host$request_uri;
}

ssl on;
ssl_certificate /var/www/ssl/.crt;
ssl_certificate_key /var/www/ssl/.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

location / { # rewrite index.php
try_files $uri /index.php$request_uri;
}

include enable-php7.2.conf; # enable php7.2
}

代理转发配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
underscores_in_headers on;
server {
listen # port;
server_name # domain;

# proxy config
location / {
proxy_pass # proxy address;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
}
}

负载均衡配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
upstream name {
service 反向服务地址,加端口
weight 权重
max_fails 失败多少次 认为主机已挂掉则,踢出
fail_timeout 踢出后重新探测时间
backup 备用服务,直接写upstream后的标识符
max_conns 允许最大连接数
slow_start 当节点恢复,不立即加入
}

# 负载均衡算法
# ip_hash : 基于Hash 计算,每个ip访问固定的stream
# 应用场景:保持session 一至性
# url_hash: (第三方)
# 应用场景:静态资源缓存,节约存储,加快速度
# least_conn 最少链接
# least_time 最小的响应时间,计算节点平均响应时间,然后取响应最快的那个,分配更高权重。

# 例子:
upstream test_stream {
ip_hash;
service 127.0.0.1:8080;
}
server {
listen 80;
server_name test.com;
location / {
proxy_pass http://test_stream;
}
}

# 了解更多
# 配置相关: https://blog.csdn.net/caijunsen/article/details/83002219
# 日志相关: https://blog.csdn.net/biubiuli/article/details/79481882

启用php7.2

需要先安装php-fpm

1
2
3
4
5
6
7
8
location ~ [^/]\.php(/|$) {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

其他配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# gzip cache
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

# browser cache
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}

location ~ .*\.(js|css)?$ {
expires 12h;
}

location ~ /\. {
deny all;
}

# location /download {
# limit_rate 1m;// Download speed limit
# limit_rate_after 30m;// Download speed limit for >30M
# }

# error_page 404 /var/www/404.html;
access_log /var/www/nginx_access.log;
error_log /var/www/nginx_error.log;
支付宝打赏 微信打赏

赞赏是不耍流氓的鼓励