1、准备工作
更新yum
yum update
yum install -y gcc gcc-c++
2、nginx 安装
(1)安装 pcre 依赖
# 下载安装包
wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
# 解压压缩包
tar –xvf pcre-8.37.tar.gz
# 编译
cd pcre-8.37
./configure
make && make install
# 查看pcre依赖是否安装成功
pcre-config --version
(2)安装 openssl 、zlib 、 gcc 依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
(3) 上传nginx压缩包到服务器,并解压编译
# 解压
tar -xvf nginx-1.24.0.tar.gz
# 进入到解压后的目录
cd nginx-1.24.0
# 检查
./configure
#编译
make && make install
注:安装成功后会在/usr目录下多出一个文件夹local/nginx,在nginx有sbin有启动脚本
3、nginx的常用操作命令
# 进入到nginx的安装目录
cd /usr/local/nginx/sbin
# 查看nginx版本号
./nginx -v
# 启动nginx
./nginx
#关闭nginx
./nginx -s stop
# 重新加载
./nginx -s reload
4、nginx的配置文件
1、位置
vim /usr/local/nginx/config/nginx.conf
2、nginx配置文件组成
#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;
# }
#}
}
1、nginx配置文件有三部分组成
第一部分 全局块:
从配置文件开始到events块之间的内容,主要设置一些影响nginx服务器整体运行配置指令
比如worker_processes 指的是并发处理的数量
第二部分events块
events涉及的指令主要影响nginx服务器与用户网络链接
比如worker_connections 1024 支持最大连接数
第三部分
5、nginx使用实例
1、nginx的反向代理
1)、nginx的反向代理(一)
# 实现效果
打开浏览器,在浏览器地址栏中输入地址,www.123.com 跳转到linux系统的tomacat主页面
# 准备工作
1、在linux系统中安装tomcat,使用默认端口号8080
注: server_name 是ip地址(nginx服务器),listen监听端口号,proxy_pass 代理地址,
2)、nginx的反向代理(二)
# 实现效果
根据不同的路径跳到不同的目标,
server {
listen 9001;
server_name 39.98.41.81;
location ~ /edu/ {
# 这里代理的url默认会把edu给去掉,
proxy_pass http://39.98.41.81:8081;
# 如果想带上edu,则用下面这种方式
# proxy_pass http://39.98.41.81:8081$request_uri;
}
location ~ /vod/ {
# 这里代理的url默认会把edu给去掉,
proxy_pass http://39.98.41.81:8080;
}
}
3)、反向代理-前后端分离
server {
listen 9002;
server_name 39.98.41.81;
root /usr/local/nginx/html/static/mubai-generator;
index index.html;
# 多个location会按照url的路径匹配
location / {
# 如果找不到会进入到这里
try_files $uri $uri/ /index.html;
}
# 静态文件的缓存配置
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 1y;
add_header Cache-Control "public";
}
# 反向代理到后端API的配置(如果需要)
location /gen/ {
# 代理后的地址会默认去掉/gen/
proxy_pass http://ip:port/mubai-generator/gen/;
}
}
2、负载均衡
3、nginx的动静分离
#进入到nginx的安装目录下
cd /usr/local/nginx/html
#创建static/www1和static/www2的目录
mkdir -p static/www1
mkdir -p static/www2
修改nginx.conf文件
访问地址http://39.98.41.81/www2/b.html
上面配置:/www1/下的路径都会被发送到/usr/local/nginx/html/static/www1下
autoindex表示开启目录,在访问时会显示文件的目录结构