nginx配置文件位于/etc/nginx/nginx.conf,Nginx 服务启动时会读入配置文件,后续的行为则按照配置文件中的指令进行。

主配置文件

# /etc/nginx/nginx.conf
user  nginx nginx;    #设置nginx用户
worker_processes  2;    #工作进程数 一般是cpu的核数
error_log  logs/error.log;    #错误日志的地址
error_log  logs/error.log  notice;    #错误记录的类型 并指定 对应文件
error_log  logs/error.log  info;    #错误记录的类型
pid        logs/nginx.pid;    #pid 存放目录
events {
    worker_connections  1024;    #每个进程最大允许的连接数
}
http {
    include       mime.types;    #设定mime类型,类型又mime.type文件定义
    default_type  application/octet-stream;    #设置默认类型
    #设置日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;    #成功日志
    sendfile        on;    #指定nginx是否有调用sendfile函数来输出文件 对于普通应用
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;    #连接超时时间
    #gzip  on;    #是否开启gzip压缩

    include /usr/local/nginx/conf/conf.d/*.conf;    #包含文件
}

子配置文件

# /etc/nginx/conf.d/wordpress.conf
#HTTP server
server {
    listen       80;
    server_name  blog.kingofzihua.top;

    if ($host = blog.kingofzihua.top) {
        return 301 https://$host$request_uri;
    }

    return 404;
}
# HTTPS server
server {
        listen       443;    #监听的端口
        server_name   blog.kingofzihua.top;    #域名 或者是IP
		# 设置SSL证书
		ssl_certificate /etc/letsencrypt/live/blog.kingofzihua.top/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/blog.kingofzihua.top/privkey.pem;
        include /etc/letsencrypt/options-ssl-nginx.conf;
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
		#定义项目根路径
        root   /www/wordpress;
        location / {
            index  index.html index.htm;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html; #错误页面
        # 错误页面所在目录
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        # 解析.php结尾的
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000; #指定解析的端口 可以写 unix:/var/run/php5-fpm.sock;
            fastcgi_index  index.php; # 指定默认访问的PHP文件
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #$document_root 就是上面设置的root
            include        fastcgi_params;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
}