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

本文详细介绍了如何使用Nginx配置301永久重定向、静态资源缓存以及HTTPS安全设置。通过配置`server_name`、`return`指令实现旧站向新站的301跳转,利用`location`与`expires`设置静态资源缓存,结合`ssl`参数启用HTTPS并优化加密协议和密钥。此外,还展示了针对PHP处理和错误页面的配置方法。
摘要由CSDN通过智能技术生成

本文主要记录了当前博客下针对静态资源、旧站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为请求参数

配置代码如下:

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为过期时间

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进行处理

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协议,加密算法支持等内容(腾讯云申请的免费证书)

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;

复制代码

完整配置如下:

#

# 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 {

}

}

复制代码

相关文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值