栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

【Nginx】常用功能配置,超简单详细

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Nginx】常用功能配置,超简单详细

本文主要内容包括nginx环境准备,创建nginx虚拟主机和搭建nginx安全网站。

Nginx环境准备

nginx源码包:nginx-1.17.6

  1. nginx源码包需要编译安装,因此需要安装编译软件:gcc,make
  2. 安装可以让nginx,支持正则表达式的软件包:pcre-devel
  3. 安装可以让nginx支持安装加密网站的软件包:openssl-devel

解包后查看nginx目录,常用目录及功能如下:

conf 存放配置文件       sbin  存放主程序        html 存放网站页面     logs  存放日志

[root@web1 lnmp_soft]# tar xf nginx-1.17.6.tar.gz   #解包
[root@web1 lnmp_soft]# cd nginx-1.17.6              #进入nginx的目录
[root@web1 nginx-1.17.6]# ls                        #查看nginix目录里的文件
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

编译安装

[root@web1 nginx-1.17.6]# ./configure --with-http_ssl_module && make && make install
checking for OS
 + Linux 3.10.0-862.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
......
......
......
......
test -d '/usr/local/nginx/html' 
        || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' 
        || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/opt/lnmp_soft/nginx-1.17.6'   

#显示离开nginx源码包所在文件位置就安装成功了

开启服务

nginx默认安装位置为/usr/local/nginx目录位置,在此目录位置下启动nginx,如果要停止nginx服务执行:/usr/local/nginx -s stop

nginx的默认端口号为80,端口开启即开启nginx服务成功

[root@web1 nginx-1.17.6]# /usr/local/nginx/sbin/nginx     #启动nginx
[root@web1 nginx-1.17.6]# ss -ntulp | grep 80             #查看nginx端口号是否开启
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=4662,fd=6),("nginx",pid=4661,fd=6))

浏览器访问nginx服务器测试,可以看到nginx欢迎页面

 修改nginx默认页面

nginx的默认页面位置在/usr/local/nginx/html里面,默认页面是index.html

[root@web1 ~]# echo 你一票我一票,王心凌80还能跳 > /usr/local/nginx/html/index.html
[root@web1 ~]# curl localhost                          
你一票我一票,王心凌80还能跳

在/usr/local/nginx/html目录下创建新的页面,名称为cxk.htm

[root@web1 ~]# echo 我会唱跳,rap和篮球! > /usr/local/nginx/html/cxk.html
[root@web1 ~]# curl localhost/cxk.html
我会唱跳,rap和篮球!

让nginx可以在浏览器中支持中文显示

修改nginx的配置文件,把nginx.conf中的 #charset koi8-r; 去掉注释并改为 charset-utf8;

[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf

 34
 35     server {
 36         listen       80;
 37         server_name  localhost;
 38
 39         charset utf-8;              #修改
 40

浏览器访问测试:

创建nginx虚拟机

假设服务提供的网站为:www.b.com

打开nginx主配置文件,在http块里面,在34~39行添加如下内容:   

 34     server {
 35         listen 80;                  //监听端口号
 36         server_name www.b.com;      //主机名
 37         root web_b;                 //存放网页的目录 
 38         index index.html;           //默认页
 39         }

修改配置文件后,重启nginx生效 

[root@web1 nginx]# /usr/local/nginx/sbin/nginx -s reload  #重启服务,服务必须是已开启状态

在nginx目录下创建www.b.com的网页目录,然后写入内容:

[root@web1 nginx]# mkdir web_b      
[root@web1 nginx]# echo this is b web > ./web_b/index.html

在本机做域名解析,让curl能解析出www.b.com

[root@web1 nginx]# vim /etc/hosts                  #文件内添加域名解析
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.4.53 www.b.com
[root@web1 nginx]# curl http://www.b.com           #测试成功!
this is b web

搭建安全网站

首先确保在安装nginx的时候,安装了安全加密的模块 --with-http_ssl_module,用 /usr/local/nginx/sbin/nginx -V 查看,安装后执行下面的步骤.

修改配置文件,启用https server,去掉103-120的注释,并且修改server_name和root 

[root@web1 nginx]# vim conf/nginx.conf
......
101     # HTTPS server
102     #
103     server {
104         listen       443 ssl;
105         server_name  www.b.com;     //虚拟主机自带ssl安全加密功能,修改成要加密的域名
106
107         ssl_certificate      cert.pem;              //证书,包含公钥
108         ssl_certificate_key  cert.key;              //私钥
109
110         ssl_session_cache    shared:SSL:1m;
111         ssl_session_timeout  5m;
112
113         ssl_ciphers  HIGH:!aNULL:!MD5;
114         ssl_prefer_server_ciphers  on;
115
116         location / {
117             root   web_b;                            //这里修改页面存储目录
118             index  index.html index.htm;
119         }
120     }
......

修改完配置文件后,创建私钥与证书:

cert.key:私钥

cert.pem:证书。证书中包含公钥,生成过程会询问诸如你在哪个国家之类

的问题,可以随意回答,但要走完全过程

创建完私钥和证书,测试安全访问nginx,看到默认页内容即成功。

[root@web1 nginx]# openssl genrsa > conf/cert.key    //创建私钥
Generating RSA private key, 2048 bit long modulus
........+++
.........................+++
e is 65537 (0x10001)
[root@web1 nginx]# openssl req -x509 -key conf/cert.key >conf/cert.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:HJ
Organizational Unit Name (eg, section) []:CC
Common Name (eg, your name or your server's hostname) []:WEB
Email Address []:111@111.com
[root@web1 nginx]# sbin/nginx -s reload              //重启nginx
[root@web1 nginx]# curl -k https://www.b.com         //检验,-k是忽略风险
this is b web

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/945065.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号