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为请求参数
配置代码如下:

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.usrequest_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_rootfastcgi_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.usrequest_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 (-frequest_filename/index.html){
            rewrite (.*) 1/index.html break;
        }
        if (-frequest_filename/index.php){
            rewrite (.*) 1/index.php;
        }
        if (!-frequest_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_FILENAMEdocument_rootfastcgi_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 (-frequest_filename/index.html){
            rewrite (.*) 1/index.html break;
        }
        if (-frequest_filename/index.php){
            rewrite (.*) 1/index.php;
        }
        if (!-frequest_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_FILENAMEdocument_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条留言