安装先决条件
sudo yum install yum-utils
安装nginx
sudo yum install -y nginx
提示没有可用的软件包,
原因是nginx位于第三方的yum源里面,而不在centos官方yum源里面
很多软件包在yum里面没有的,解决的方法,就是使用epel源,也就是安装epel-release软件包。EPEL (Extra Packages for Enterprise Linux)是基于Fedora的一个项目,为“红帽系”的操作系统提供额外的软件包,适用于RHEL、CentOS等系统。可以在下面的网址上找到对应的系统版本,架构的软件包
- 解决办法,安装epel
sudo yum install epel-release
yum update
重试yum install -y nginx
Nginx默认目录
# whereis命令用于查找文件
# 该指令会在特定目录中查找符合条件的文件。这些文件应属于原始代码、二进制文件,或是帮助文件。
# 该指令只能用于查找二进制文件、源代码文件和man手册页,一般文件的定位需使用locate命令
# whereis [-bfmsu][-B <目录>...][-M <目录>...][-S <目录>...][文件...]
whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man3/nginx.3pm.gz /usr/share/man/man8/nginx.8.gz
执行目录:/usr/sbin/nginx
模块所在目录:/usr/lib64/nginx/modules
配置所在目录:/etc/nginx/
默认站点目录:/usr/share/nginx/htm
- 启动、停止、重载命令
# 启动
systemctl start nginx.service
# 停止
systemctl stop nginx.service
# 重载
systemctl reload nginx.service
#查看状态
systemctl status nginx.service
- 查看端口情况
netstat -antp | grep : # 查看所有端口
netstat -antp | grep :80 # 查看所有80端口
提示-bash: netstat: 未找到命令
安装# yum -y install net-tools
再次执行netstat -antp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 49436/nginx: master
tcp 0 0 192.168.72.100:43272 202.232.140.10:80 TIME_WAIT -
tcp 0 0 192.168.72.100:44148 150.65.7.130:80 TIME_WAIT -
tcp 0 0 192.168.72.100:44760 103.48.119.119:80 TIME_WAIT -
tcp 0 0 192.168.72.100:44402 45.120.52.95:80 TIME_WAIT -
tcp 0 0 192.168.72.100:55758 134.160.38.1:80 TIME_WAIT -
tcp 0 0 192.168.72.100:47554 14.17.102.85:80 TIME_WAIT -
tcp 0 0 192.168.72.100:50954 103.233.192.20:80 TIME_WAIT -
tcp 0 0 192.168.72.100:51502 109.224.14.75:80 TIME_WAIT -
tcp6 0 0 :::80 :::* LISTEN 49436/nginx: master
- 查看进程状态
ps aux | grep nginx
root 49525 0.0 0.1 105504 2128 ? Ss 12:21 0:00 nginx: master process /usr/sbin/nginx
nginx 49526 0.0 0.1 105972 2924 ? S 12:21 0:00 nginx: worker process
nginx 49527 0.0 0.1 105972 2924 ? S 12:21 0:00 nginx: worker process
nginx 49528 0.0 0.1 105972 2924 ? S 12:21 0:00 nginx: worker process
nginx 49529 0.0 0.1 105972 2924 ? S 12:21 0:00 nginx: worker process
nginx 49530 0.0 0.1 105972 2924 ? S 12:21 0:00 nginx: worker process
nginx 49531 0.0 0.1 105972 2924 ? S 12:21 0:00 nginx: worker process
nginx 49532 0.0 0.1 105972 2924 ? S 12:21 0:00 nginx: worker process
nginx 49533 0.0 0.1 105972 2924 ? S 12:21 0:00 nginx: worker process
root 49535 0.0 0.0 112824 976 pts/0 S+ 12:22 0:00 grep --color=auto nginx
- 查看日志,具体文件位置在
nginx.conf
配置
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
Nginx配置
- 配置文件
cat /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 192.168.72.100;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://192.168.72.100:9000;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
#精准匹配测试
#第1,2条虽然匹配,但第三条是精准匹配,出第三条结果
#测试路径/equal/a/b/c
location ~ /equal/* {
echo '/equal/*';
}
location /equal/a/b {
echo '/equal/a/b';
}
location = /equal/a/b/c {
echo '/equal/a/b/c';
}
#普通匹配测试
#第1,2条虽然匹配,第三条匹配更长,出第三条结果
#测试路径/match/a/b/c
location /match/a {
return 200 "/match/a";
}
location /match/a/b {
return 200 "/match/a/b";
}
location /match/a/b/c {
return 200 "/match/a/b/c";
}
location /match/a/b/c/d {
return 200 "/match/a/b/c/d";
}
#正则匹配覆盖普通匹配,不会覆盖非正则匹配
#访问/re/a.htm,会被后面的正则覆盖
#访问/re/a/b开头的路径,不会被后面的正则覆盖
location /re/a.htm {
echo 'match /re/a.htm';
}
location ^~ /re/a/b {
echo 'math ^~/re/a/b*';
}
location ~ /re/(.*)\.(htm|js|css)$ {
echo "cover /re/$1.$2";
}
#正则匹配成功一条后,便不再走其它正则
#测试路径/rex/a/b/c.htm
location ~ /rex/.*\.(htm|js|css)$ {
echo "match first";
}
location ~ /rex/a/(.*)\.(htm|js|css)$ {
echo "match second";
}
location ~ /rex/a/b/(.*)\.(htm|js|css)$ {
echo "match third";
}
#规则一:
#访问路径:http://192.168.72.100/user/query?id=1
location /user {
#path1:/user path2:/query
#ip:port 后面无 /
proxy_pass http://192.168.0.105:8080;
}
规则二:
#访问路径 :http://192.168.72.100/A/user/query?id=1
location /A/user {
#path1:/A/user path2:/query
#ip:port 后面有 /xxx
proxy_pass http://192.168.0.105:8080/user;
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /404.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}