版本:nginx.x86_64 1:1.14.0-1.el7_4.ngx
# cat nginx.conf
# 定义Nginx运行的用户和用户组
user nginx;
# nginx进程数,建议设置为等于CPU总核心数
worker_processes 1;
# 全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log warn;
#error_log logs/error.log info;
# 进程文件
pid /var/run/nginx.pid;
# 工作模式与连接数上限
# worker_connections是单个后台worker process进程的最大并发链接数,
# 并发总数是 worker_processes 和 worker_connections 的乘积,
# 即 max_clients = worker_processes * worker_connections
events {
worker_connections 1024;
}
http {
# 文件扩展名与文件类型映射表
include /etc/nginx/mime.types;
# 默认文件类型
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 /var/log/nginx/access.log main;
# 开启高效文件传输模式,
# sendfile指令指定nginx是否调用sendfile函数来 输出文件,对于普通应用设为 on,
# 如果用来进行下载等应用磁盘IO重负载应用,可设置 为off,
# 以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常 把这个改成off。
sendfile on;
# 防止网络阻塞
#tcp_nopush on;
# 长连接超时时间,单位是秒
keepalive_timeout 65;
#gzip on;
# 包含的子配置文件
include /etc/nginx/conf.d/*.conf;
}
让我们去看看包含路径下有什么
# cd /etc/nginx/conf.d/
# ls
default.conf
发现,是示例文件 default.conf
# cat default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/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 /usr/share/nginx/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;
#}
}
官方文档:server部分点此查看
我又创建了一个新的,来写自己的server配置
server{
# 监听端口号
listen 80;
#server_name 支持:
# 通配符名称
server {
listen 80;
server_name example.org www.example.org;
*.example.org;
mail.*;
~^(?<user>.+)\.example\.net$;
...
}
# 正则表达式名称
server {
listen 80;
server_name ~^(www\.)?(.+)$;
...
}
# 其他名称/杂项名称(至于这个空的,我没用到过,官方文档理由解释,我不太懂:If someone makes a request using an IP address instead of a server name, the “Host” request header field will contain the IP address and the request can be handled using the IP address as the server name:)
server {
listen 80;
server_name example.org
www.example.org
""
192.168.1.1
;
...
}
# 国际化名称
server_name localhost;
其他
server {
listen 80 default_server;
server_name _;
return 404;
}
There is nothing special about this name, it is just one of a myriad of invalid domain names which never intersect with any real name. Other invalid names like “--
” and “!@#
” may equally be used.
这个名字没有什么特别的,它仅仅是一个许多无效的域名中的一个代表与任何真实的名字,永远不会相交。其它无效的名称,如“ - “ 和” !@# “也可同样使用。
default_server:nginx的虚拟主机是通过HTTP请求中的Host值来找到对应的虚拟主机配置,如果找不到呢?那 nginx就会将请求送到指定了 default_server 的 节点来处理
也就是对于未绑定的域名指向你的服务器时,匹配不到你配置的虚拟主机域名后,会默认使用这个虚拟主机,然后直接返回404。
把这个配置添加你的nginx.conf即可。
注意,修改了配置文件后最好先检查一下修改过的配置文件是否正 确,以免重启后Nginx出现错误影响服务器稳定运行。判断Nginx配置是否正确命令如下:
nginx -t -c /usr/nginx/conf/nginx.conf
# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful