一、配置文件
nginx 安装完成后,配置文件放在conf目录下,每个配置文件提供了一份备份文件.default文件
nginx.conf 为主配置文件
[root@localhost nginx]# cd conf/ [root@localhost conf]# pwd /usr/local/nginx/conf -rw-r--r--. 1 root root 1077 Sep 4 17:19 fastcgi.conf -rw-r--r--. 1 root root 1077 Sep 4 17:19 fastcgi.conf.default -rw-r--r--. 1 root root 1007 Sep 4 17:19 fastcgi_params -rw-r--r--. 1 root root 1007 Sep 4 17:19 fastcgi_params.default -rw-r--r--. 1 root root 2837 Sep 4 17:19 koi-utf -rw-r--r--. 1 root root 2223 Sep 4 17:19 koi-win -rw-r--r--. 1 root root 3957 Sep 4 17:19 mime.types -rw-r--r--. 1 root root 3957 Sep 4 17:19 mime.types.default -rw-r--r--. 1 root root 2656 Sep 4 17:19 nginx.conf -rw-r--r--. 1 root root 2656 Sep 4 17:19 nginx.conf.default -rw-r--r--. 1 root root 636 Sep 4 17:19 scgi_params -rw-r--r--. 1 root root 636 Sep 4 17:19 scgi_params.default -rw-r--r--. 1 root root 664 Sep 4 17:19 uwsgi_params -rw-r--r--. 1 root root 664 Sep 4 17:19 uwsgi_params.default -rw-r--r--. 1 root root 3610 Sep 4 17:19 win-utf
1、配置文件结构
#user nobody; worker_processes 1; 配置Nginx的工作进程数,一遍设为cpu总核数或者总核数2倍 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; 配置nginx允许单个进程并发连接的最大请求数 } http { include mime.types; include用于引入配置文件 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; 默认为值on 表示开启高效文件传输模式 #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65;设置长连接超时时间(单位秒) #gzip on; server { listen 80;监听端口 默认监听端口80端口 server_name localhost; 设置主机域名 #charset koi8-r; #access_log logs/host.access.log main; location / { root html;设置主机站点根目录地址 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 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
Nginx:主配置文件主要由 main、events、http、server 和 location 5个块组成
main 主要控制Nginx子进程所属的用户和用户组,派生子进程数、错误日志位置与级别、pid位置、子进程优先级、进程对应cpu 进程能够打开 的文件描述连接符数目等。
events 控制nginx处理连接方式
http nginx处理http请求的主要配置块,大多数配置都在这里进行
server nginx中主机的配置块,可用于配置多个虚拟主机
location server 中对应目录级别的控制块,可以有多个。
Nginx指令由名称和参数组成,当一个指令中含有多个子指令作为参数时,使用{}进行包裹 使用 ; 号进行结尾
2、设置用户和组
Nginx服务由一个主进程 和多个工作进程组成。
主进程以root权限运行,工作进程默认以nobody用户运行,(nobody账号是一个不能登录的账号,有一个专用id,可以将每个运行的工作进程进行隔离,避免黑客攻击造成影响其他数据)
[root@localhost sbin]# ps aux|grep nginx root 10413 0.0 0.0 45848 1120 ? Ss 11:14 0:00 nginx: master process ./nginx nobody 10414 0.0 0.0 48372 1964 ? S 11:14 0:00 nginx: worker process root 10604 0.0 0.0 112652 956 pts/0 R+ 11:14 0:00 grep --color=auto nginx
nginx提供2种设置用户和组的方式,一种是在安装时通过编译选项进行设置,另一种是修改配置文件,(注意,2种方式都需要提前创建好用户和组)
1、编译安装配置方式
在./configure 编译安装nginx时选项中,添加2个选项
--user=<user>
--group=<group>
user 指定用户名称
group 指定用户所在组的名称
--user=www \
--group=www \
2、修改配置文件方式
#user nobody;
user www www;
修改完毕后,保存nginx.conf 配置文件 平滑重启。
检查是否修改成功
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
重启nginx
# /usr/local/nginx/sbin/nginx -s reload
3、自定义错误页
nginx主配置文件中,给出以下处理方式:
error_page 500 502 503 504 /50x.html;
50x.html 表示当发生上述指定的任意错误时,都使用网站目录下的50x.html文件处理
3-1、为每种类型的错误设置单独的处理方式
#error_page 500 502 503 504 /50x.html; error_page 403 /40x.html; # 指定网站根目录下的页面40x.html 处理403错误 error_page 404 /404.jpg ; # 指定网站根目录下的图片404.jpg 处理404错误
在网站根目录下创建404.jpg图片和40x.html文件
image.png
3-2利用在线资源进行处理错误
# 处理单个指定错误
error_page 403 http://example.com/forbidden.html;
# 处理一系列指定错误
error_page 500 502 503 504 http://example.com/forbidden.html
3-3 更改响应状态码
隐藏服务器返回的真实状态码信息,可以利用=进行自定义设置
error_page 404 =200 /40x.html; #隐藏服务器返回的真实状态码信息