Nginx配置文件
Nginx配置文件
nginx.conf
default
#user nobody;
worker_processes 1;
#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;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 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;
# }
#}
}
以下将原有的注释全部删除了
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm; #访问80端口,找到nginx目录下的html目录下的index.html
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- worker_processes
worker_processes 1; 默认为1,表示开启一个业务进程
- worker_connections
worker_connections 1024; 单个业务进程可接受连接数
- include mime.types;
include mime.types; 引入http mime类型
- default_type application/octet-stream;
default_type application/octet-stream; 如果mime类型没匹配上,默认使用二进制流的方式传输。
- sendfile on;
sendfile on; 使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝。
未开启sendfile
开启后
- keepalive_timeout 65;
keepalive_timeout 65; ,保持连接,超时时间。
- server
虚拟主机配置 vhost
server {
listen 80; 监听端口号
server_name localhost; #域名、主机名
location / { 匹配路径
root html; 文件根目录
index index.html index.htm; 默认页名称
}
error_page 500 502 503 504 /50x.html; 报错编码对应页面
location = /50x.html {
root html;
}
}
使用host文件解析域名(利用host文件假装解析域名)
修改完成后
访问虚拟机的ip解析的域名
还可以通过域名解析 然后连接到内网
我们在阿里云的dns域名解析上面添加添加了我们域名和内网ip的对应关系,仅仅只是对应关系,所以我们ping域名可以解析成我们的内网ip,由于终端与对应机器在同一局域网所以能通,你换个不在同一内网的不行
虚拟主机
原本一台服务器只能对应一个站点,通过虚拟主机技术可以虚拟化成多个站点同时对外提供服务
虚拟主机域名配置
创建weixiao目录,在里面创建两个文件夹,hello,hi,分别创建index.html
修改/usr/local/nginx/conf下的nginx.config
记得开启相对于的端口号,,,踩坑了。。
firewall-cmd --zone=public --add-port=88/tcp --permanent
firewall-cmd --reload
重加载:systemctl reload nginx
重启:systemctl restart nginx
结果:
注意:
虚拟主机技术server中,相同的主机端口号会报错。
可以这样配置:listen 80; server_name www.website.com;
listen 88; server_name qqq.website.com;
在通配符的server_name *.mmban.com
设置下,可以访问不同的网站
域名解析规则
- servername匹配规则
我们需要注意的是servername匹配分先后顺序,写在前面的匹配上就不会继续往下匹配了。
- 完整匹配
我们可以在同一servername中匹配多个域名
server_name vod.mmban.com www1.mmban.com;
- 通配符匹配
server_name *.mmban.com
- 通配符结束匹配
server_name vod.*;
- 正则匹配
server_name ~^[0-9]+.mmban.com$;