侧边栏壁纸
博主头像
如此肤浅博主等级

但行好事,莫问前程!

  • 累计撰写 24 篇文章
  • 累计创建 12 个标签
  • 累计收到 6 条评论

目 录CONTENT

文章目录

Nginx | 安装 | 配置

如此肤浅
2022-04-24 / 4 评论 / 1 点赞 / 2,171 阅读 / 3,019 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-05-15,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

以下采用Docker方式进行安装!

安装

  1. 拉取镜像
docker pull nginx
  1. 以守护方式运行
docker run -id --name nginx -p 80:80 nginx	
  1. 验证安装成功
    在浏览器输入ip地址:80,若出现下图则安装成功:
    image

基本操作

配置目录挂载

采用文件挂载的方式将容器中Nginx配置目录挂载在宿主机上,修改宿主机中配置目录中的文件就等于修改容器中Nginx配置目录中的文件。

  1. 将容器中Nginx配置目录,拷贝到宿主机上
# 目录/usr/local/nginx_conf要存在
docker cp nginx:/etc/nginx /usr/local/nginx_conf
  1. 以守护方式创建容器,并挂载配置目录
docker run -di --name nginx -p 80:80 -v /usr/local/mydata/nginx_conf/:/etc/nginx 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;
#}
1

评论区