一、nginx下载安装

版本nginx 1.15.5

系统环境centos7.5(本机ip192.168.199.228)

关闭selinux 和防火墙firewall

1、下载

wget http://nginx.org/download/nginx-1.15.5.tar.gz -P /usr/src

2、安装

安装大概过程

配置---编译---安装

配置 1)检查环境 是否 满足安装条件 依赖解决 2)指定安装方式 配置文件 命令文件 各种文件放哪里 开启模块功能【内 置模块 三方模块】 3)指定软件安装在那里

a、切换到usr/src目录,解压文件

  1. [root@localhost src]# cd /usr/src
  2. [root@localhost src]# ls
  3. debug kernels nginx-1.15.5.tar.gz
  4. [root@localhost src]# tar xf nginx-1.15.5.tar.gz
  5. [root@localhost src]# ls
  6. debug kernels nginx-1.15.5 nginx-1.15.5.tar.gz
  7. [root@localhost src]#

  

查看配置方法

  1. [root@localhost src]# pwd
  2. /usr/src
  3. [root@localhost src]# cd nginx-1.15.5
  4. [root@localhost nginx-1.15.5]# ls
  5. auto CHANGES.ru configure html man src
  6. CHANGES conf contrib LICENSE README
  7. [root@localhost nginx-1.15.5]# ./configure --help #查看配置参数帮助

  

b、安装各种依赖环境

  1. [root@localhost src]# cd nginx-1.15.5
  2. [root@localhost nginx-1.15.5]# yum -y install gcc pcre-devel zlib zlib-devel
  3. Loaded plugins: fastestmirror
  4. Loading mirror speeds from cached hostfile
  5. * base: mirror.vpshosting.com.hk
  6. * extras: centos.01link.hk
  7. * updates: hk.mirrors.thegigabit.com
  8. Resolving Dependencies
  9. --> Running transaction check
  10. ---> Package gcc.x86_64 0:4.8.5-39.el7 will be installed
  11. ...

  

gcc 编译工具

pcre-devel 在nginx中url 需要用到这个包

zlib zlib-devel 解压缩工具

 对于 gcc,因为安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境的话,需要安装gcc。

  对于 pcre,prce(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

  对于 zlib,zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

c、指定目录进行编译

  1. [root@localhost nginx-1.15.5]# ./configure --prefix=/usr/local/nginx
  2. checking for OS
  3. + Linux 3.10.0-862.el7.x86_64 x86_64
  4. checking for C compiler ... found
  5. + using GNU C compiler
  6. + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
  7. ...

  

d、编译make

make就是将源码进行编译生成可执行程序的过程

  1. [root@localhost nginx-1.15.5]# pwd
  2. /usr/src/nginx-1.15.5
  3. [root@localhost nginx-1.15.5]# ls
  4. auto CHANGES.ru configure html Makefile objs src
  5. CHANGES conf contrib LICENSE man README
  6. [root@localhost nginx-1.15.5]# make
  7. make -f objs/Makefile
  8. make[1]: Entering directory `/usr/src/nginx-1.15.5'
  9. cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
  10. ...

  

没有error的话进行

e、make install完成安装

  1. [root@localhost nginx-1.15.5]# make install
  2. make -f objs/Makefile install
  3. make[1]: Entering directory `/usr/src/nginx-1.15.5'
  4. test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
  5. test -d '/usr/local/nginx/sbin' \
  6. ...

  

完成安装

二、nginx的相关目录

  1. nginx path prefix: "/usr/local/nginx" #nginx的安装目录
  2. nginx binary file: "/usr/local/nginx/sbin/nginx" #nginx的启动文件
  3. nginx modules path: "/usr/local/nginx/modules" # nginx的模块目录
  4. nginx configuration prefix: "/usr/local/nginx/conf" #nginx的配置文件位置
  5. nginx configuration file: "/usr/local/nginx/conf/nginx.conf" #nginx的配置文件全路径
  6. nginx pid file: "/usr/local/nginx/logs/nginx.pid" #nginx的进程号
  7. nginx error log file: "/usr/local/nginx/logs/error.log" #nginx的错误日志目录
  8. nginx http access log file: "/usr/local/nginx/logs/access.log" #nginx的访问日志目录

  

三、nginx的启动与关闭、检查配置文件

查看端口是否占用

方法一

安装netstat 用netstat进行查看

  1. [root@localhost nginx]# yum -y install net-tools
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. ...


  5. [root@localhost nginx]# netstat -ntpl
  6. Active Internet connections (only servers)
  7. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  8. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 881/sshd
  9. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1128/master
  10. tcp6 0 0 :::22 :::* LISTEN 881/sshd
  11. tcp6 0 0 ::1:25 :::* LISTEN 1128/master

  

方法二

安装lsof 用lsof 查看

  1. [root@localhost nginx]# yum -y install lsof
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. ...

  5. [root@localhost nginx]# lsof -i :80
  6. [root@localhost nginx]#
  7. #没有显示结果表示端口没有被占用

  

启动nginx方式

  1. [root@localhost nginx]# lsof -i :80
  2. [root@localhost nginx]# /usr/local/nginx/sbin/nginx #启动nginx
  3. [root@localhost nginx]# lsof -i :80
  4. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  5. nginx 11728 root 6u IPv4 38674 0t0 TCP *:http (LISTEN)
  6. nginx 11729 nobody 6u IPv4 38674 0t0 TCP *:http (LISTEN)
  7. [root@localhost nginx]# #nginx把80端口占用了

  

查看是否安装成功

方法一:

用google浏览器地址栏输入http://192.168.199.228(nginx安装服务器的ip地址)

如果出现Welcome to nginx!页面则安装成功

方法二

用elinks 查看安装是否成功,elinks不会有缓存,一般的google浏览器会有缓存

  1. [root@localhost nginx]# yum -y install elinks
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. * base: mirror.vpshosting.com.hk
  5. * extras: centos.01link.hk
  6. * updates: hk.mirrors.thegigabit.com
  7. ...

  8. [root@localhost nginx]# elinks http://192.168.199.228 --dump
  9. Welcome to nginx!

  10. If you see this page, the nginx web server is successfully installed and
  11. working. Further configuration is required.

  12. For online documentation and support please refer to [1]nginx.org.
  13. Commercial support is available at [2]nginx.com.

  14. Thank you for using nginx.

  15. References

  16. Visible links
  17. 1. http://nginx.org/
  18. 2. http://nginx.com/
  19. [root@localhost nginx]#

  

关闭 nginx

  有3种方式:

  方式1:快速停止

  1. cd /usr/local/nginx/sbin
  2. ./nginx -s stop

  

  此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。不太友好。

  方式2:平缓停止

  1. cd /usr/local/nginx/sbin
  2. ./nginx -s quit

  

  此方式是指允许 nginx 服务将当前正在处理的网络请求处理完成,但不在接收新的请求,之后关闭连接,停止工作。

方法3:killall

  1. killall nginx

  

  相当于直接杀死所有的关于nginx的进程

重启 nginx

  方式1:先停止再启动

  1. ./nginx -s quit
  2. ./nginx

  

  相当于先执行停止命令再执行启动命令。

  方式2:重新加载配置文件

  1. ./nginx -s reload

  

  1.  

  通常我们使用nginx修改最多的便是其配置文件 nginx.conf。修改之后想要让配置文件生效而不用重启 nginx,便可以使用此命令。

  方法3:

  

  1. killall -s HUP nginx

  

  检测配置文件语法是否正确

  方式1:通过如下命令,指定需要检查的配置文件

  1. nginx -t -c /usr/local/nginx/conf/nginx.conf

  

  方式2:通过如下命令,不加 -c 参数,默认检测nginx.conf 配置文件。

  1. nginx -t

  

四、Nginx配置文件详解

nginx文件结构

  1. ... #全局块

  2. events { #events块
  3. ...
  4. }

  5. http #http块
  6. {
  7. ... #http全局块
  8. server #server块
  9. {
  10. ... #server全局块
  11. location [PATTERN] #location块
  12. {
  13. ...
  14. }
  15. location [PATTERN]
  16. {
  17. ...
  18. }
  19. }
  20. server
  21. {
  22. ...
  23. }
  24. ... #http全局块
  25. }

  

1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。

2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。

5、location块:配置请求的路由,以及各种页面的处理情况

  该指令用于匹配 URL。

  语法如下:

  1. location [ = | ~ | ~* | ^~] uri {
  2.  
  3. }

  

  (1)= :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。

  (2)~:用于表示 uri 包含正则表达式,并且区分大小写。

  (3)~*:用于表示 uri 包含正则表达式,并且不区分大小写。

  (4)^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。

  注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识。

location匹配规则补充

  1. 语法规则: 【= | ^~ | ~ | ~* | / | /uri
  2. location = /uri = 表示精确匹配,只有完全匹配上才能生效,若找到,停止搜索;
  3. location ^~ /uri ^~开头表示对URL路径进行前缀匹配,并且在正则匹配之前,若找到,停止搜索;
  4. location ~ pattern ~开头表示区分大小写的正则匹配,按配置文件顺序匹配;
  5. location ~* pattern ~*开头表示不区分大小写的正则匹配,按配置文件顺序匹配;
  6. location /uri 不带任何修饰符,表示前缀匹配,在正则匹配之后;
  7. location / 通用匹配,任何未匹配到其他location的请求都会匹配到,相当于default
  8.  
  9. 多个location配置的情况匹配顺序为
  10. 首先精确匹配 =
  11. 其次前缀匹配 ^~;
  12. 其次是按照配置文件中的正则匹配;
  13. 然后匹配不带任何修饰符的前缀匹配;
  14. 最后交给/通用匹配;

  

示例

  1. location支持的语法优先级:
  2.  
  3. location匹配顺序
  4.  
  5. # www.abc.com/
  6. 1.location = / {
  7. 我是代码1
  8. } 精确匹配
  9.  
  10. # www.abc.com/images/
  11. 2.location ^~ /images/ {
  12. 我是代码2
  13. } 匹配常规串,不做正则检查
  14.  
  15. # www.abc.com/xxx.gif
  16. #www.abc.com/xxx.jpg
  17. #www.abc.com/xxx.gif
  18. #www.abc.com/xxx.jpeg
  19.  
  20. 3.location ~* \.(gif|jpg|jpeg) {
  21. 我是代码3
  22. } 正则匹配
  23.  
  24. #优先级为4, www.abc.com/doc/xx资源
  25. 4. location /doc/ {
  26. 我是代码4
  27. } 匹配常规字符,有正则优先正则
  28.  
  29. #如果你谁都没匹配到的话,默认走/,走网页根目录,优先级最低
  30.  
  31. 5.location / {
  32. 我是代码5
  33. } 所有的location都不匹配后,默认匹配

  

Nginx配置文件位置/usr/local/nginx/conf/nginx.conf

  1. 详解一
  1. [root@localhost conf]# vi nginx.conf

  2. #启动该程序的默认用户
  3. #user nobody;
  4. #一个主进程和多个工作进程。工作进程是单进程的,且不需要特殊授权即可运行;这里定义的是工作进程数量
  5. worker_processes 4;

  6. #全局错误日志的位置及日志格式
  7. #error_log logs/error.log;
  8. #error_log logs/error.log notice;
  9. #error_log logs/error.log info;

  10. #pid logs/nginx.pid;


  11. events {
  12. #每个工作进程最大的并发数,设置的工作进程数*每个进程允许的最多线程数就是最大并发数
  13. worker_connections 1024;

  14. #http服务器设置
  15. http {
  16. #设定mime类型,类型由mime.type文件定义
  17. include mime.types;
  18.  
  19. default_type application/octet-stream;
  20. #日志格式
  21. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  22. # '$status $body_bytes_sent "$http_referer" '
  23. # '"$http_user_agent" "$http_x_forwarded_for"';
  24. #$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
  25. #$remote_user:用来记录客户端用户名称;
  26. #$time_local: 用来记录访问时间与时区;
  27. #$request: 用来记录请求的url与http协议;
  28. #$status: 用来记录请求状态;成功是200,
  29. #$body_bytes_sent :记录发送给客户端文件主体内容大小;
  30. #$http_referer:用来记录从那个页面链接访问过来的;
  31. #$http_user_agent:记录客户浏览器的相关信息;
  32.  

  33. #全局访问日志路径
  34. #access_log logs/access.log main;
  35.  
  36. #sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
  37. sendfile on;
  38.  
  39. #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
  40. #tcp_nopush on;
  41.  
  42. #长连接超时时间
  43. #keepalive_timeout 0;
  44. keepalive_timeout 65;
  45.  
  46. #开启压缩
  47. #gzip on;
  48.  
  49. #配置虚拟主机
  50. server {
  51. #虚拟主机使用的端口
  52. listen 80;
  53.  
  54. #虚拟主机域名
  55. server_name localhost;

  56. #虚拟主机支持的字符集
  57. #charset koi8-r;

  58. #虚拟主机的访问日志路径
  59. #access_log logs/host.access.log main;

  60. #定义web根路径
  61. location / {
  62. #根目录路径
  63. root html;
  64. #索引页
  65. index index.html index.htm;
  66. }
  67.  
  68. #404页面配置
  69. #error_page 404 /404.html;

  70. # redirect server error pages to the static page /50x.html
  71. #
  72. #根据错误码 返回对应的页面
  73. error_page 500 502 503 504 /50x.html;
  74. #定义页面路径
  75. location = /50x.html {
  76. root html;
  77. }
  78.  
  79. #定义反向代理服务器 数据服务器是lamp模型
  80. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  81. #
  82. #location ~ \.php$ {
  83. # proxy_pass http://127.0.0.1;
  84. #}
  85.  
  86. #定义PHP为本机服务的模型
  87. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  88. #
  89. #location ~ \.php$ {
  90. # root html;
  91. # fastcgi_pass 127.0.0.1:9000;
  92. # fastcgi_index index.php;
  93. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  94. # include fastcgi_params;
  95. #}
  96. # concurs with nginx's one
  97. #
  98. #location ~ /\.ht {
  99. # deny all;
  100. #}
  101. }


  102. #
  103. #server {
  104. # listen 8000;
  105. # listen somename:8080;
  106. # server_name somename alias another.alias;

  107. # location / {
  108. # root html;
  109. # index index.html index.htm;
  110. # }
  111. #}
  112.  
  113. #https的配置方案
  114. # HTTPS server
  115. #
  116. #server {
  117. # listen 443 ssl;
  118. # server_name localhost;

  119. # ssl_certificate cert.pem;
  120. # ssl_certificate_key cert.key;

  121. # ssl_session_cache shared:SSL:1m;
  122. # ssl_session_timeout 5m;

  123. # ssl_ciphers HIGH:!aNULL:!MD5;
  124. # ssl_prefer_server_ciphers on;

  125. # location / {
  126. # root html;
  127. # index index.html index.htm;
  128. # }
  129. #}

  130. }

  

详解二

  1. ########### 每个指令必须有分号结束。#################
  2. #user administrator administrators; #配置用户或者组,默认为nobody nobody。
  3. #worker_processes 2; #允许生成的进程数,默认为1
  4. #pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
  5. error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
  6. events {
  7. accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
  8. multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
  9. #use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
  10. worker_connections 1024; #最大连接数,默认为512
  11. }
  12. http {
  13. include mime.types; #文件扩展名与文件类型映射表
  14. default_type application/octet-stream; #默认文件类型,默认为text/plain
  15. #access_log off; #取消服务日志
  16. log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
  17. access_log log/access.log myFormat; #combined为日志格式的默认值
  18. sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
  19. sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
  20. keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。

  21. upstream mysvr {
  22. server 127.0.0.1:7878;
  23. server 192.168.10.121:3333 backup; #热备
  24. }
  25. error_page 404 https://www.baidu.com; #错误页
  26. server {
  27. keepalive_requests 120; #单连接请求上限次数。
  28. listen 4545; #监听端口
  29. server_name 127.0.0.1; #监听地址
  30. location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
  31. #root path; #根目录
  32. #index vv.txt; #设置默认页
  33. proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
  34. deny 127.0.0.1; #拒绝的ip
  35. allow 172.18.5.54; #允许的ip
  36. }
  37. }
  38. }

  

上面是nginx的基本配置,需要注意的有以下几点:

1、1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址; 2.$remote_user :用来记录客户端用户名称; 3.$time_local : 用来记录访问时间与时区;4.$request : 用来记录请求的url与http协议;

5.$status : 用来记录请求状态;成功是200, 6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;7.$http_referer :用来记录从那个页面链接访问过来的; 8.$http_user_agent :记录客户端浏览器的相关信息;

2、惊群现象:一个网路连接到来,多个睡眠的进程被同事叫醒,但只有一个进程能获得链接,这样会影响系统性能。

3、每个指令必须有分号结束。

4、修改user 时要用useradd 添加用户,创建一个不能从终端登录的名字为webuser的系统用户

  1. [root@localhost conf]# useradd -s /sbin/nologin -r webuser

  

五、nginx的默认网站

当Nginx配置文件中有且只有一个Server的时候,该Server就被Nginx认为是默认网站,所有发给Nginx服务器器80端口的数据都会默认给该Server.

默认网站设置

  1. server {
  2. listen 80;
  3. server_name localhost;

  4. #charset koi8-r;

  5. #access_log logs/host.access.log main;

  6. location / {
  7. root html;
  8. index index.html index.htm;
  9. }

  10. #error_page 404 /404.html;

  11. # redirect server error pages to the static page /50x.html
  12. #
  13. error_page 500 502 503 504 /50x.html;
  14. location = /50x.html {
  15. root html;
  16. }

  17. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  18. #
  19. #location ~ \.php$ {
  20. # proxy_pass http://127.0.0.1;
  21. #}

  22. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  23. #
  24. #location ~ \.php$ {
  25. # root html;
  26. # fastcgi_pass 127.0.0.1:9000;
  27. # include fastcgi_params;
  28. #}

  29. # deny access to .htaccess files, if Apache's document root
  30. # concurs with nginx's one
  31. #
  32. #location ~ /\.ht {
  33. # deny all;
  34. #}
  35. }

  

nginx默认网站的访问控制

创建环境,在html文件夹里创建abc三个文件分别写入index.html

  1. [root@localhost html]# ls
  2. 50x.html index.html
  3. [root@localhost html]# pwd
  4. /usr/local/nginx/html
  5. [root@localhost html]# mkdir a b c
  6. [root@localhost html]# ls
  7. 50x.html a b c index.html
  8. [root@localhost html]# echo aaa >a/index.html
  9. [root@localhost html]# echo bbb >b/index.html
  10. [root@localhost html]# echo ccc >c/index.html
  11. [root@localhost html]# ls a/
  12. index.html
  13. [root@localhost html]# cat a/index.html
  14. aaa
  15. [root@localhost html]# ls
  16. 50x.html a b c index.html
  17. [root@localhost html]# elinks http://192.168.199.228/a --dump #本机访问测试
  18. aaa

  

用例1 :ip控制

针对a文件夹只允许本机访问,拒绝其他所有人访问

设置修改nginx配置文件,本机ip192.168.199.228

  1. [root@localhost html]# pwd
  2. /usr/local/nginx/html
  3. [root@localhost html]# vi ../conf/nginx.conf
  4. ...
  5. http{
  6. ...

  7. server{
  8. listen 80;
  9. server_name localhost;

  10. #charset koi8-r;
  11. charset utf-8;

  12. #access_log logs/host.access.log main;

  13. location / {
  14. root html;
  15. index index.html index.htm;
  16. };
  17. #location / 这里的/代表网站的根目录
  18.  
  19. #针对a文件夹进行设置;
  20. location /a {
  21. allow 127.0.0.1;
  22. allow 192.168.199.228;
  23. deny all;
  24. #return 404;
  25. #return http://www.jd.com;
  26. #可以返回指定错误页,也可以进行url跳转,注意这里的返回是访问成功和不成功的都返回
  27. }
  28.  
  29. }

  

allowdeny会按照顺序, 从上往下, 找到第一个匹配规则, 判断是否允许访问, 所以一般把all放最后。

其他例子

  1.   deny 192.168.1.1;
      allow 192.168.1.0/24;
      allow 10.1.1.0/16;
      allow 2001:0db8::/32;
      deny all;

测试修改后的配置文件是否有误

  1. [root@localhost html]# pwd
  2. /usr/local/nginx/html
  3. [root@localhost html]# ../sbin/nginx -g ../conf/nginx.conf
  4. nginx: [emerg] unexpected end of parameter, expecting ";" in command line
  5. [root@localhost html]#

  

修改完后方法一:修改完配置一定要检测Nginx配置是否正确,正确后再重新软加载配置文件

  1. [root@localhost html]# ../sbin/nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  4. [root@localhost html]# ../sbin/nginx -s reload

  

修改完后方法二:不再直接kill后再重启,直接重新加载nginx的配置文件

  1. [root@localhost html]# killall -s HUP nginx
  2. [root@localhost html]# elinks http://192.168.199.228/a --dump
  3. aaa

  

用例2:登录验证

针对b文件夹,任何人都可以访问,但是需要凭用户密码进行验证

设置修改nginx配置文件,本机ip192.168.199.228

  1. [root@localhost html]# pwd
  2. /usr/local/nginx/html
  3. [root@localhost html]# vi ../conf/nginx.conf
  4. ...
  5. http{
  6. ...

  7. server{
  8. listen 80;
  9. server_name localhost;

  10. #charset koi8-r;
  11. charset utf-8;

  12. #access_log logs/host.access.log main;

  13. location / {
  14. root html;
  15. index index.html index.htm;
  16. };
  17. #location / 这里的/代表网站的根目录
  18.  
  19. #针对a文件夹进行设置;
  20. location /a {
  21. allow 127.0.0.1;
  22. allow 192.168.199.228;
  23. deny all;
  24. #return 404;
  25. #return http://www.jd.com;
  26. #可以返回指定错误页,也可以进行url跳转,注意这里的返回是访问成功和不成功的都返回
  27. }
  28.  
  29. #针对b文件夹进行设置;
  30. location /b {
  31. auth_basic ”登陆验证test";
  32. auth_basic_user_file /etc/nginx/htpasswd;
  33. #auth_basic_user_file 用来存储用户认证信息的文件;
  34. }
  35.  
  36. }

  

语法讲解: auth_basic 默认关闭,开启的话输入一段字符串即可。 auth_basic_user_file 该文件存储用户账号密码。

安装httpd-tools使用htpasswd工具生成认证信息文件放置在上面设置的位置

  1. [root@localhost html]# yum -y install httpd-tools
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. * base: hk.mirrors.thegigabit.com
  5. * extras: hk.mirrors.thegigabit.com
  6. * updates: hk.mirrors.thegigabit.com
  7. ...


  8. [root@localhost html]# mkdir /etc/nginx
  9. [root@localhost html]# htpasswd -c /etc/nginx/htpasswd user1
  10. #创建文件htpasswd并将新用户user1和加密的密码写入文件到/etc/nginx/目录下
  11. New password:
  12. Re-type new password:
  13. Adding password for user user1
  14. [root@localhost html]# htpasswd -m /etc/nginx/htpasswd user2
  15. #如果htpasswd文件已经存在则使用-m参数添加新用户账户密码
  16. New password:
  17. Re-type new password:
  18. Adding password for user user2
  19. [root@localhost html]# cat /etc/nginx/htpasswd
  20. user1:$apr1$C8hzuJ.t$z8ZI/y4HgrbrhnmC1QkTp/
  21. user2:$apr1$5LB3P1Wj$HxyEELRZ3vDogTGM3xR2E.
  22. [root@localhost html]#

  

重新加载配置文件验证效果

  1. [root@localhost html]# killall -s HUP nginx
  2. [root@localhost html]#

  

参考链接

[1]https://www.cnblogs.com/knowledgesea/p/5175711.html

nginx之旅(第一篇):nginx下载安装、nginx启动与关闭、nginx配置文件详解、nginx默认网站的更多相关文章

  1. 从零开始使用git第一篇:下载安装配置

    从零开始使用git 第一篇:下载安装配置 第一篇:从零开始使用git第一篇:下载安装配置 第二篇:从零开始使用git第二篇:git实践操作 第三篇:从零开始使用git第三篇:git撤销操作.分支操作和 ...

  2. zabbix安装配置agent程序之agent配置文件详解

    安装zabbix-agent http://repo.zabbix.com/zabbix/3.2/rhel/6/x86_64/ 下载:zabbix-agent-3.2.0-1.el6.x86_64.r ...

  3. 【转】nginx服务器安装及配置文件详解

    原文:http://seanlook.com/2015/05/17/nginx-install-and-config/ nginx服务器安装及配置文件详解 nginx在工作中已经有好几个环境在使用了, ...

  4. nginx高性能WEB服务器系列之四配置文件详解

    nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...

  5. Nginx配置配置文件详解

    文章目录 配置文件 nginx.conf配置文件详解 用于调试.定位问题的配置参数 正常运行必备的配置参数 优化性能的配置参数 事件相关配置 Fastcgi相关配置参数 常需要调整的参数 nginx作 ...

  6. 一、Nginx配置文件详解

    配置文件介绍 主要有两部分:分别是 main:主体部分 http{}:虚拟主机配置部分 配置指令主要以分号结尾:配置语法:directive value1 [value2 ....] 支持使用的变量 ...

  7. ubuntu nginx 安装以及配置文件详解

    1.到nginx官网下载源码包.最好下载稳定版本,nginx官网http://www.nginx.org/ 2.安装nginx依赖包运行命令: sudo apt-get install libssl- ...

  8. nginx安装及配置文件详解

    一)nginx安装及模块讲解 1.1.nginx安装步骤 mkdir /soft wget http://nginx.org/download/nginx-1.12.0.tar.gz tar zxf ...

  9. 第一篇博客 安装open live writer

    第一篇博客安装open live writer http://openlivewriter.org/ 有的人可能会打不开,所以我准备了一个百度云的链接地址 链接:https://pan.baidu.c ...

随机推荐

  1. Socket是什么(一)

    网络编程就是编写程序使两台联网的计算机相互交换数据. 那么,这两台计算机之间用什么传输数据呢?首先需要物理连接.如今大部分计算机都已经连接到互联网,因此不用担心这一点. 在此基础上,只需要考虑如何编写 ...

  2. ASP.NET CoreMVC 中的视图

    ASP.NET Core MVC 中的视图 MVC 中的视图 用于显示Controller提供给它的 Model 的业务数据. 视图是带有嵌入 Razor 标记的 HTML 模板. 如果编程语言是 C ...

  3. mysql出生日期转成年龄

    可以直接用数据库函数进行转换,省去java代码转换的麻烦 SELECT  TIMESTAMPDIFF(YEAR, '1988/01/10', CURDATE()) 且此函数容错很好,就算是null,‘ ...

  4. Ubuntu 14.04 apt-get update失效解决(转)

    现象如下: VirtualBox:~$ sudo apt-get update Err http://mirrors.aliyun.com trusty InRelease Err http://mi ...

  5. jar解压后重新打包

    因为一些原因修改了jar中的配置文件,但用WinRAR压缩成zip文件后该后缀名为jar,发现重新压缩的文件不可用,所有这些情况下我们必须用jar重新打包. 配置Java环境,让jar命令可用: ja ...

  6. sql server 2019 & spark

    https://cloudblogs.microsoft.com/sqlserver/2019/04/01/how-to-develop-and-submit-spark-jobs-to-sql-se ...

  7. python数据分析开发中的常用整理

    Pandas操作 python使用pandas读取csv import pandas as pd #数据筛选 usetTable = pd.read_csv(filename,header = 0) ...

  8. 用Java编程能给物联网(IoT)带来什么优势与不同?

    用Java编程能给物联网(IoT)带来什么优势与不同? 这是一个不太容易回答的问题,也是一个适合拿出来与大家讨论的一个话题~首先需要聊聊物联网硬件与嵌入式设备有什么不同.嵌入式设备通常是一个软件一体的 ...

  9. 小记:iterator && auto

    小记:iterator && auto iterator 众所周知,我们有一种强大的东西,它叫做STL,比如queue.vector.set.map.multimap .deque等. ...

  10. 集合类源码(七)Map(ConcurrentHashMap, ConcurrentSkipListMap, TreeMap)

    ConcurrentHashMap 内部结构 在JDK1.8之前的实现结构是:ReentrantLock+Segment+HashEntry+链表 JDK1.8之后的实现结构是:synchronize ...