Nginx
下载
# 解压
tar -zxvf nginx-1.20.2.tar.gz
# 剪切
mv nginx-1.20.2 /usr/local/nginx
# 进入nginx目录
cd /usr/local/nginx
# 没有gcc环境,安装
yum install -y gcc
#
./configure --prefix=/usr/local/nginx
# 安装perl
yum install -y pcre pcre-devel
# 安装
yum install -y zlib zlib-devel
看到这个就是success
# 编译
make & make install
# 启动
cd sbin
./nginx ../conf/nginx.conf
查看
ps -ef|grep nginx
访问:success
命令行参数
# 启动
/usr/local/nginx/sbin/nginx -c nginx.conf
# 停止
/usr/local/nginx/sbin/nginx -s stop
# 重新加载
/usr/local/nginx/sbin/nginx -s reload
# 版本查看
/usr/local/nginx/sbin/nginx -v
# 检测文件错误
/usr/local/nginx/sbin/nginx -t -c 文件名
# 端口查看
netstat -anp | more
配置文件
nginx.conf
# 全局快
#user nobody;
# 工作进程数目 通常等于CPU数量或2*CPU数量
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块
events {
# 单个进程最大连接数
worker_connections 1024;
}
# http块
http {
# 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块
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# location块
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.conf
,代理到8081
此处虚拟机内需要自行开启tomcat 8081
的服务,否则无效。
server {
listen 80;
# 代理服务器ip
server_name 192.168.10.129;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
# 代理到服务器端口为8081的应用
proxy_pass http://127.0.0.1:8081;
index index.html index.htm;
}
...
}
# 重新reload
/usr/local/nginx/sbin/nginx -s reload
host文件配置
,可选
192.168.10.129 www.lcq.com
访问:http://www.lcq.com或http://192.168.10.129
location
- 精准匹配:
=
- 普通匹配:存在
^~
直接返回,否则匹配多个取最长 - 正则匹配:
~
,~*
,!~
,!~*
,匹配成功直接返回
负载均衡
访问相同功能的服务(集群)
nginx.conf
...
upstream servername{
#轮询
#server ip:port;
#server ip:port;
#权重
server ip:port weight=1;
server ip:port weight=2;
}
...
server {
...
location / {
proxy_pass http://servername;
}
}
...
servername
不可以用下划线命名
动静分离
动态资源放置tomcat服务器,静态资源存放在nginx
nginx.conf
server {
...
location /edu/ {
#root html;
#proxy_pass http://127.0.0.1:8081;
proxy_pass http://myserver;#动态资源
index index.html index.htm;
}
location /edu/img/ {
root /usr/local/nginx/html;#静态资源
}
...
}