• nginx知识点-1


    #因为是最小化安装,先安装vim编辑器,net-tools查看端口,psmisc可以使用killall命令bash-completion tab补全命令(需要重启生效)
    
    1. [root@localhost ~]# yum -y install net-tools psmisc vim bash-completion
    2. [root@localhost ~]# tar zxvf nginx-1.17.6.tar.gz
    3. #因为等下要源码安装nginx,所以先安装gcc make编译软件,pcre-devel是为了使nginx支持正则表达式,openssl-devel是为了nginx加密
    4. [root@localhost ~]# cd nginx-1.17.6/ && ls
    5. auto     CHANGES.ru configure html     Makefile objs   src
    6. CHANGES conf       contrib   LICENSE man       README
    7. [root@localhost ~]# yum -y install gcc make pcre-devel openssl-devel
    8. [root@localhost nginx-1.17.6]# ./configure --prefix=/usr/local/nginx --user=nginx --with-http_ssl_module && make && make install
    9. --prefix 指定安装位置
    10. --user 指定以哪位用户身份启动nginx
    11. --with-http-ssl_module 使用安全网站模块
    12. [root@localhost nginx-1.17.6]# cd /usr/local/nginx/
    13. [root@localhost nginx]# ls
    14. conf html logs sbin
    15. [root@localhost nginx]# sbin/nginx
    16. nginx: [emerg] getpwnam("nginx") failed 启动失败的原因是,编译时指定以nginx用户启动,服务器没有nginx用户,所以失败
    17. [root@localhost nginx]# useradd nginx #创建个nginx用户
    18. [root@localhost nginx]# sbin/nginx
    19. [root@localhost nginx]# netstat -ntupl |grep 80
    20. tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4615/nginx: master
    nginx认证

    访问nginx网站时,需要输入正确的用户名和密码才能访问

    配置如下:

    1. [root@localhost nginx]# pwd
    2. /usr/local/nginx
    3. [root@localhost nginx]# vim conf/nginx.conf
    4. server {
    5.       listen       80;
    6.       server_name localhost;
    7.       auth_basic "password";
    8.       auth_basic_user_file "/usr/local/nginx/pass"; 认证文件
    9. [root@localhost nginx]# sbin/nginx -s reload
    10. htpasswd用于为指定用户生成基于网页用户身份认证的密码,由httpd-tools软件包提供。支持3种加密算法:MD5、SHA和系统上的crypt()函数,不指定算法时,默认为md5。
    11. [root@localhost ~]# yum -y install httpd-tools
    12. [root@localhost ~]# htpasswd -c pass tom
    13. New password:
    14. Re-type new password:
    15. Adding password for user tom
    16. [root@localhost nginx]# ls
    17. client_body_temp fastcgi_temp logs proxy_temp scgi_temp
    18. conf             html         pass sbin       uwsgi_temp
    19. [root@localhost nginx]# cat pass
    20. tom:$apr1$KVns/c9N$K3YF4Lnb3lM2nMcH/WF1r/
    21. 添加第二个用户认证以上,不需要加 -c
    22. [root@localhost nginx]# htpasswd pass jerry
    23. New password:
    24. Re-type new password:
    25. Adding password for user jerry
    26. [root@localhost nginx]# cat pass
    27. tom:$apr1$KVns/c9N$K3YF4Lnb3lM2nMcH/WF1r/
    28. jerry:$apr1$e/pzkrYu$90EooPydjHbG.fzc8Na6c1

    浏览器访问ip地址

    虚拟主机
    可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响。

    配置如下:

    基于域名的虚拟主机

    1. [root@localhost nginx]# vim conf/nginx.conf
    2. server {
    3.       listen 80;
    4.       server_name www.b.com;
    5.       root html_b;
    6.       index index.html;
    7.       }
    8.   server {
    9.       listen       80;
    10.       server_name www.a.com;
    11.        #charset koi8-r;
    12.        #access_log logs/host.access.log main;
    13.       location / {
    14.           root   html_a;
    15.           index index.html index.html;
    16. [root@localhost nginx]# mkdir html_b && echo "test-b~~~" > html_b/index.html
    17. [root@localhost nginx]# mkdir html_a && echo "test_a" > html_a/index.html
    18. [root@localhost nginx]# sbin/nginx -s reload
    19. 这里没有搭建DNS服务器,就用主机映射文件暂时替用
    20. [root@localhost nginx]# vim /etc/hosts
    21. 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    22. ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    23. 192.168.99.5 www.a.com
    24. 192.168.99.5 www.b.com
    25. [root@localhost nginx]# curl www.b.com
    26. test-b~~~
    27. [root@localhost nginx]# curl www.a.com
    28. test_a

    基于ip的虚拟主机

    1. [root@localhost nginx]# ifconfig eth0 | sed -n "/inet/p" | awk '{print $2}' |grep ^1
    2. 192.168.88.5
    3. [root@localhost nginx]# ifconfig eth1 | sed -n "/inet/p" | awk '{print $2}' |grep ^1
    4. 192.168.99.5
    5. [root@localhost nginx]# vim conf/nginx.conf
    6. server {
    7.       listen 80;
    8.       server_name 192.168.99.5;
    9.       root html_b;
    10.       index index.html;
    11.       }
    12.   server {
    13.       listen       80;
    14.       server_name  192.168.88.5;
    15.        #charset koi8-r;
    16.        #access_log logs/host.access.log main;
    17.       location / {
    18.           root   html_a;
    19.           index index.html index.htm;
    20.       }
    21. [root@localhost nginx]# sbin/nginx -s reload
    22. [root@localhost nginx]# curl 192.168.88.5
    23. test_a
    24. [root@localhost nginx]# curl 192.168.99.5
    25. test-b~~~

    基于端口的虚拟主机

    1. [root@localhost nginx]# vim conf/nginx.conf
    2. server {
    3.       listen 88;
    4.       server_name www.a.com;
    5.       root html_b;
    6.       index index.html;
    7.       }
    8.   server {
    9.       listen       80;
    10.       server_name www.a.com;
    11.        #charset koi8-r;
    12.        #access_log logs/host.access.log main;
    13.       location / {
    14.           root   html_a;
    15.           index index.html index.htm;
    16.       }
    17. [root@localhost nginx]# sbin/nginx -s reload
    18. [root@localhost nginx]# curl www.a.com:88
    19. test-b~~~
    20. [root@localhost nginx]# curl www.a.com:80
    21. test_a
    nginx加密网站
    https协议原理 首先,客户端与服务器建立连接,各自生成私钥和公钥,是不同的。服务器返给客户端一个公钥,然后客户端拿着这个公钥把要搜索的东西加密,称之为密文,并连并自己的公钥一起返回给服务器,服务器拿着自己的私钥解密密文,然后把响应到的数据用客户端的公钥加密,返回给客户端,客户端拿着自己的私钥解密密文,把数据呈现出来

    配置如下:

    1. [root@localhost nginx]# vim conf/nginx.conf
    2. 输入法切换英文,按esc,冒号:,输入以下情况
    3. :101,118s/#/ /
    4. 把以下配置的#号取消注释
    5. server {
    6.         listen       443 ssl;
    7.         server_name localhost;
    8.         ssl_certificate     cert.pem;
    9.         ssl_certificate_key cert.key;
    10.         ssl_session_cache   shared:SSL:1m;
    11.         ssl_session_timeout 5m;
    12.         ssl_ciphers HIGH:!aNULL:!MD5;
    13.         ssl_prefer_server_ciphers on;
    14.         location / {
    15.             root   https;
    16.             index index.html index.htm;
    17.         }
    18.     }
    19. [root@localhost nginx]# openssl genrsa > conf/cert.key
    20. Generating RSA private key, 2048 bit long modulus
    21. ...+++
    22. .+++
    23. e is 65537 (0x10001)
    24. openssl genrsa 命令是会用来生成 RSA 私有秘钥,不会生成公钥,因为公钥提取自私钥。生成时是可以指定私钥长度和密码保护。
    25. [root@localhost nginx]# openssl req -x509 -key conf/cert.key > conf/cert.pem
    26. -key:指定已有的秘钥文件生成秘钥请求
    27. req命令主要的功能有,生成证书请求文件, 查看验证证书请求文件,还有就是生成自签名证书
    28. -x509: 说明生成自签名证书,自签名证书又称为根证书,是自己颁发给自己的证书,即证书中的颁发者和主体名相同。
    29. You are about to be asked to enter information that will be incorporated
    30. into your certificate request.
    31. What you are about to enter is what is called a Distinguished Name or a DN.
    32. There are quite a few fields but you can leave some blank
    33. For some fields there will be a default value,
    34. If you enter '.', the field will be left blank.
    35. -----
    36. Country Name (2 letter code) [XX]:dc
    37. State or Province Name (full name) []:dc
    38. Locality Name (eg, city) [Default City]:dc
    39. Organization Name (eg, company) [Default Company Ltd]:dc
    40. Organizational Unit Name (eg, section) []:dc
    41. Common Name (eg, your name or your server's hostname) []:dc
    42. Email Address []:dc
    43. [root@localhost nginx]# mkdir https && echo "https-test~~" > https/index.html
    44. [root@localhost nginx]# sbin/nginx -s reload
    45. [root@localhost nginx]# curl -k https://192.168.99.5
    46. https-test~~
  • 相关阅读:
    Java面试八股之myBatis动态SQL的作用
    【微服务架构】微服务不是魔术:处理超时
    阿里巴巴开源限流组件Sentinel初探之集成Gateway
    软考高级软件架构师论文——论软件架构评估
    Mac安装JDK
    Java8新特性
    GitHub 标星 120K 的 Java 面试知识点总结,真就物超所值了
    Grafana系列-统一展示-7-ElasticSearch数据源
    太空 5G 在启动板上
    js第五章
  • 原文地址:https://blog.csdn.net/weixin_65562581/article/details/134257385