1,官网 nginx news下载Nginx stable (稳定版) version
To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo
with the following contents:
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
yum -y install nginx
systemctl enable nginx
systemctl start nginx #这里如果起不来检查其他服务端口之类的冲突
http://192.168.31.74/ #起来之后在浏览器可以直接访问IP
vim /usr/share/nginx/html/index.html #默认访问网页文件
cat /etc/nginx/nginx.conf #主配置文件 官方的配置文件内容比较少
user nginx; #nginx 所运行的用户身份
worker_processes auto; #你的CPU有几核就启动几个worker进程,这样性能最好尽可能的让一个worker捆绑一个CPU,尽量减少切换的过程
error_log /var/log/nginx/error.log notice; #错误日志 日志级别:notice
pid /var/run/nginx.pid;
events {
worker_connections 1024; #每一个worker能有多少个连接
}
http { #定义站点的内容
include /etc/nginx/mime.types; #支持的资源类型
default_type application/octet-stream; # 如果找不到资源类型 就以下载的方式传输给你
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #日志格式 $remote_addr:远程地址 $remote_user:用户名 $request :请求
'$status $body_bytes_sent "$http_referer" '
#'$status:状态码 $body_bytes_sent:body发送了多少 $http_referer:referer是什么(告诉服务器该网页是从哪个页面链接过来的,服务器因此可以获得一些信息用于处理)
'"$http_user_agent" "$http_x_forwarded_for"'; #$http_user_agent :来源的设备 $http_x_forwarded_for :
access_log /var/log/nginx/access.log main; #日志放在哪里 使用上面定义的 main格式
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf; #链接到的文件
}
cat /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#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;
#}
}
# 负责接受http请求 ,过来之后要匹配 Host:a.com字段是什么域名,从而进入那个server段
http { #定义站点的内容
server { #负责一个域名
listen 80;
server_name a.com; #请求的时候写域名会更具这个字段进行匹配
#匹配请求的uri
location / { #请求不同的路径找不同的文件
root /code;
index index.html
}
}
server {
listen 80;
server_name b.com;
}
server {
listen 80;
server_name c.com;
}
}
vim /etc/nginx/conf.d/game.oldxu.com.conf 定义一个网站
server {
listen 80;
server_name game.oldxu.com;
location / {
root /code;
index index.html;
}
}
nginx -t #检查语法
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
systemctl reload nginx #重载 nginx
mkdir /code
cd /code
wget http://fj.xuliangwei.com/public/html5.zip #下载一个游戏
unzip html5.zip #解压
192.168.31.74 game.oldxu.com #打开windows,hosts文件添加这一行
#然后直接访问域名可以看到页面
#点开对应的小游戏会发现url为:http://game.oldxu.com/game/jianren/index.html
#这个地址对应:http://game.oldxu.com/game/jianren/index.html
2,实现虚拟目录
vim /etc/nginx/conf.d/test.oldxu.com.conf
server {
listen 80;
server_name test.oldxu.com;
location / {
root /test;
index index.html;
}
}
systemctl reload nginx
mkdir /test
echo "hello Test" > /test/index.html
Nginx常用模块
1,ngx_http_index_module模块找不到文件时,通常会将请求发给ngx_http_autoindex_module模块。
server {
listen 80;
server_name mirror.oldxu.com;
location / {
root /var/ftp;
autoindex on; #开启此模块 如果找不到路径就会默认找 上面的 /var/ftp
autoindex_exact_size off; #开启文件字节大小统计
autoindex_localtime on; #时间设置为24小时制
charset 'utf-8'; #解决中文乱码问题 一般不建议使用中文
}
location /admin {
alias /code; #这样可以跳转到 code
index index.html;
}
location /admin {
allow 192.168.31.72; #允许这个ip
deny all; #拒绝其他所有IP
}
}