NGINX下运行静态资源、PHP应用及支持HTTPS等配置详

本文主要记录了当前博客下针对静态资源、旧站301跳转、Https配置等一系列内容,nginx以server块来确定某一部分虚拟域名及相关配置,所以我们可以在server块中配置server_name虚拟域名,access_log访问日志,return跳转,root项目根目录,location匹配url做相应操作,error_page错误页面,listen监听端口,include包含配置文件以及其他的一些ssl等操作,下面总结一下当前所使用内容

301&302跳转

原有旧站blog.congcong.us等都301跳转到www.congcong.us

$scheme为当前的协议
$request_uri为请求参数
配置代码如下:

1
2
3
4
5
6
复制代码server {
server_name congcong.us blog.congcong.us;
access_log /var/log/nginx/www.access.log;
#root /usr/share/nginx/html/blogtemp;
return 301 $scheme://www.congcong.us$request_uri;
}

静态资源配置缓存

配置图片及css等内容根据需要进行缓存 针对图片的请求 access_log不进行记录 expires为过期时间

1
2
3
4
5
6
7
8
9
复制代码location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ { 
access_log off;
expires 30d;
}

location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
access_log off;
expires 24h;
}

配置php,及配置php的url美化

过滤所有的url 如果说非以index.php结尾,那么增加这个进行rewrite

过滤所有的php结尾内容 转交由php-fpm进行处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
复制代码location / {

if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}

location ~ \.php$ {
root /usr/share/nginx/html/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 1000;
include fastcgi_params;
}

配置Https,进行SSL配置

监听443端口,ssl配置开启,关联crt与key,设置ssl协议,加密算法支持等内容(腾讯云申请的免费证书)

1
2
3
4
5
6
7
8
复制代码listen 443;
ssl on;
ssl_certificate "/xxx/xxx/1_www.congcong.us_bundle.crt";
ssl_certificate_key "/xxx/xxx/2_www.congcong.us.key";
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;

完整配置如下:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
复制代码#
# The default server
#
server {
server_name congcong.us blog.congcong.us;
access_log /var/log/nginx/www.access.log;
return 301 $scheme://www.congcong.us$request_uri;
}

server {
server_name projects.congcong.us kindle.congcong.us;
access_log /var/log/nginx/pk.access.log;
return 302 $scheme://congcong.us;
}


server {
listen 443;
server_name www.congcong.us;
root /xxx/xxx/xxx/xxx/wordpress;
index index.php index.html index.htm;

# Load configuration files for the default server block.
include /xxx/xxx/default.d/*.conf;

location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
access_log off;
expires 30d;
}

location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
access_log off;
expires 24h;
}


location / {

if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}

location ~ \.php$ {
root /xxx/xxx/xxx/xxx/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 1000;
include fastcgi_params;
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}

ssl on;
ssl_certificate "/xxx/xxx/1_www.congcong.us_bundle.crt";
ssl_certificate_key "/xxx/xxx/2_www.congcong.us.key";
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;

}

server {
listen 80 default_server;
server_name www.congcong.us;
root /xxx/xxx/xxx/xxx/wordpress;
index index.php index.html index.htm;

# Load configuration files for the default server block.
include /xxx/xxx/default.d/*.conf;

location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
access_log off;
expires 30d;
}

location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
access_log off;
expires 24h;
}


location / {

if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location ~ \.php$ {
root /xxx/xxx/xxx/xxx/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 1000;
include fastcgi_params;
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

相关文章

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%