下载其他需要的工具
安装Nginx
创建文件夹
下载Nginx
解压
configure配置
编译
放行端口号
启动nginx
配置Nginx
路由配置(负载均衡)
配置
动静分离
创建文件夹
配置
Docker搭建
拉取镜像
创建文件夹
新建资源文件夹
新建配置文件夹
配置文件 nginx.conf
配置文件 default.conf
放行端口号
运行容器
yum -y install gcc gcc-c++ pcre-devel zlib-devel openssl openssl-devel
cd /usr/local/nginx
wget https://nginx.org/download/nginx-1.15.7.tar.gz
tar -zxvf nginx-1.15.7.tar.gz
cd /usr/local/nginx/nginx-1.15.7
./configure --prefix=/usr/local/nginx
make && make install
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
./usr/local/nginx/sbin/nginx
ps -ef|grep nginx
kill -9 nginx
./usr/local/nginx/sbin/nginx -t
./nginx -s reload
强制
./nginx -s stop
完整
./nginx -s quit
#user nobody;
#开启进程数 <=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 {
#单个进程最大连接数(并发量)(总并发=单进程并发量数x进程数),每个worker允许同时产生多少个链接.
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压缩
#gzip on;
#http的协议版本
#gzip_http_version 1.0;
#如果是IE的话,就关闭压缩
#gzip_disable 'MSIE [1-6].';
#需要压缩的文件格式
#gzip_types image/jepg;
# 一个网站虚拟机
server {
#端口,默认是80端口
listen 80;
#域名
server_name localhost;
#网站根目录
#root /usr/local/nginx/html
#charset koi8-r;
#nginx访问日志,使用main格式(可以自定义格式)
#access_log logs/host.access.log main;
location / {
#root指定nginx的根目录为/usr/local/nginx/html
root html;
#默认访问文件,欢迎页先去html目录下找index.html,如果找不到再去找index.htm
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#错误页面及其返回地址,错误码为500、502、503、504都会返回50.html
error_page 500 502 503 504 /50x.html;
#location后面是"=",则是精确匹配
location = /50x.html {
root html;
}
#缓存图片文件
# location ^.*\[jgp|png] { #缓存图片类型:jgp|png等
#缓存时间1天 d:天 h:小时
# expired 1d;
#}
# 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;
# }
#}
}
upstream www.tomcat.com {
server 192.168.0.109:8080 weight=1; #权重,值越大,被访问到的机会就越大.
server 192.168.0.110:8080 weight=2;
#ip_hash; #负载均衡算法:ip_hash算法,默认使用轮询算法.
#server 192.168.0.109:8080 backup; #设为备机,服务并发特别大的时候,才会生效.
}
location / {
proxy_pass http://www.tomcat.com; # http协议开头
}
#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;
upstream www.tomcat.com {
server 192.168.0.109:8080 weight=1; #权重,值越大,被访问到的机会就越大.
server 192.168.0.110:8080 weight=2;
#ip_hash; #负载均衡算法:ip_hash算法,默认使用轮询算法.
#server 192.168.0.109:8080 backup; #设为备机,服务并发特别大的时候,才会生效.
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://www.tomcat.com; # http协议开头
}
#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;
# }
#}
}
./usr/local/nginx/sbin/nginx -s reload
http://192.168.0.109/
mkdir -p /usr/local/static/jcs
配置
location ~.*\.(js|css)$ {
root /usr/local/static/jcs;
expires 1d;
}
location ~.*\.(jpg|img|jepg|wmv|wma|text|avi|mp3|mp4|flv)$ {
root /home/myftp;
expires 10d;
}
#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;
#upstream www.tomcat.com {
# server 192.168.0.109:8080 weight=1; #权重,值越大,被访问到的机会就越大.
# server 192.168.0.110:8080 weight=2;
# #ip_hash; #负载均衡算法:ip_hash算法,默认使用轮询算法.
# #server 192.168.0.109:8080 backup; #设为备机,服务并发特别大的时候,才会生效.
# }
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# location / {
# proxy_pass http://www.tomcat.com; # http协议开头
#}
#location / {
# root html;
# index index.html index.htm;
#}
location ~.*\.(js|css)$ {
root /usr/local/static/jcs;
expires 1d;
}
location ~.*\.(jpg|img|jepg|wmv|wma|text|avi|mp3|mp4|flv)$ {
root /home/myftp;
expires 10d;
}
#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;
# }
#}
}
./usr/local/nginx/sbin/nginx -s reload
docker pull nginx
mkdir -p /usr/local/docker-nginx/html
chmod 777 /usr/local/docker-nginx/html
mkdir -p /usr/local/docker-nginx/conf.d
mkdir -p /usr/local/docker-nginx/conf
mkdir -p /usr/local/docker-nginx/log
chmod 777 /usr/local/docker-nginx/conf.d
chmod 777 /usr/local/docker-nginx/conf
chmod 777 /usr/local/docker-nginx/log
vim /usr/local/docker-nginx/conf/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
vim /usr/local/docker-nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html 6666.jpg;
}
#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;
#}
}
放行端口号
firewall-cmd --zone=public --add-port=8081/tcp --permanent
firewall-cmd --reload
1
2
运行容器
日志文件位置:/var/log/nginx
配置文件位置: /etc/nginx
资源存放的位置: /usr/share/nginx/html
docker run --name nginx -p 8081:80 -v /usr/local/docker-nginx/html:/usr/share/nginx/html -v /usr/local/docker-nginx/log:/var/log/nginx -v /usr/local/docker-nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v /usr/local/docker-nginx/conf.d:/etc/nginx/conf.d --privileged=true -d nginx
转载来源:https://blog.csdn.net/Amor_Leo/article/details/85225075