以下采用Docker方式进行安装!
安装
- 拉取镜像
docker pull nginx
- 以守护方式运行
docker run -id --name nginx -p 80:80 nginx
- 验证安装成功
在浏览器输入ip地址:80,若出现下图则安装成功:
基本操作
配置目录挂载
采用文件挂载的方式将容器中Nginx配置目录挂载在宿主机上,修改宿主机中配置目录中的文件就等于修改容器中Nginx配置目录中的文件。
- 将容器中Nginx配置目录,拷贝到宿主机上
# 目录/usr/local/nginx_conf要存在
docker cp nginx:/etc/nginx /usr/local/nginx_conf
- 以守护方式创建容器,并挂载配置目录
docker run -di --name nginx -p 80:80 -v /usr/local/mydata/nginx_conf/:/etc/nginx nginx
配置详解
全局配置
# 每个配置项由配置指令和指令参数 2 个部分构成
#user nobody; # 指定Nginx Worker进程运行以及用户组
worker_processes 1; # worker就是处理请求的,一般数量和CPU核心数一致
#error_log logs/error.log; # 错误日志的存放路径 和错误日志对应日志的级别
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; # 进程PID存放路径
# 事件模块指令,用来指定Nginx的IO模型,Nginx支持的有select、poll、kqueue、epoll 等。不同的是epoll用在Linux平台上,而kqueue用在BSD系统中,对于Linux系统,epoll工作模式是首选
events {
use epoll;
# 定义Nginx每个进程的最大连接数, 作为服务器来说: worker_connections * worker_processes,
# 作为反向代理来说,最大并发数量应该是worker_connections * worker_processes/2。因为反向代理服务器,每个 并发会建立与客户端的连接和与后端服务的连接,会占用两个连接
worker_connections 1024;
}
主机配置(server)
# 虚拟主机的配置
server {
listen 80; # 虚拟主机的服务端口
server_name localhost; #用来指定IP地址或域名,多个域名之间用空格分开
#charset koi8-r;
#access_log logs/host.access.log main;
#URL地址匹配
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; //拦截PHP结尾的跳转到对应路径
#}
# 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;
#}
}
例如:我想要把localhost:8080的服务通过localhost:80访问 (当然你也可以使用域名)
server {
listen 80; //监听端口
server_name localhost; //转发地址 可以填写你的域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://localhost:8080; //代理地址
}
#URL地址匹配
location / {
root html; # 服务默认启动目录
index index.html index.htm; #默认访问文件,按照顺序找
}
}
# HTTPS server //https 加密协议
#
#server {
# listen 443 ssl; //监听端口
# server_name localhost;
# ssl_certificate cert.pem; //ssl证书秘钥地址
# ssl_certificate_key cert.key; //ssl证书公钥地址
# 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;
# }
#}
location配置
#URL地址匹配
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;
#}
评论区