nginx - nginx的配置文件 - 虚拟主机

目录

1. nginx 的master进程和worker进程

2. nginx的配置文件

2.1 主配置文件 nginx.conf

2.1.1 主配置文件的作用:

2.1.2  主配置文件的一些常用配置

2.2   日志文件 logs

2.3  网页文件 html

3. 虚拟主机

基于IP的虚拟主机   :一个网站对应一个公网IP基于端口的虚拟主机   : 一个网站对应一个端口

3.1  基于域名的虚拟主机

3. 2   基于域名的虚拟主机配置

 示例:添加域名 www.liming.com

3.2.1  修改配置文件,添加www.liming.com的配置

3.2.2  新建相关目录和文件

3.2.3  检测语法,刷新nginx服务

3.2.4   修改hosts文件

linux系统下面:


1. nginx 的master进程和worker进程

master是worker的父进程

当我们的客户端连接过来是连接到worker进程上面的,master进程是管理worker进程的,master进程不管连接

 master进程管理worker进程,当worker进程死掉会重启一个,
当master进程死掉,worker进程依然会存在(会屏蔽hup信号),nginx服务还可以正常访问,但是没有master进程以后再杀死worker进程,worker进程不会再重启进程,

########################################### 

2. nginx的配置文件

2.1 主配置文件 nginx.conf

2.1.1 主配置文件的作用:

作用:就是给nginx进程提供参数的,管理员希望nginx进程按照我们的要求去工作,所以我们更改了配置文件要刷新一下服务,让nginx进程重新加载配置文件,按照修改了的配置文件的内容去工作

###########################################  

2.1.2  主配置文件的一些常用配置

[root@www conf]# cat nginx.conf

#user  nobody;
# 开启worker进程的数量,和cpu核心数量一致
worker_processes  2;

# 将nginx的master进程号记录到 nginx.pid文件里面
pid        logs/nginx.pid;

# 表示一个worker进程启动2048 个线程,并发数量
events {
    worker_connections  2048;
}

# http协议相关的。
http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    # 启用压缩功能 --》加快传输的速度的
    #gzip  on;
    # 提供web服务的配置 --》虚拟主机--》网站
    server {
    # 监听80端口
	listen  80;
    # 网站服务的域名
	server_name www.liming.com;

        #charset koi8-r;
        # 访问日志的路径和格式
        access_log  logs/liming.access.log  main;
        # 提供某个路由的根目录 -->/ 访问网站的根目录
        location / {
            # html是存放网页的根目录
            root   html;
            # 指定首页
            index  index.html index.htm;
        }
        # 错误页面,访问不到网页的时候会给用户返回这个页面
        error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        # 出现500 502 503 504 错误的时候返回这个页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


    }



}

###########################################  

日志格式

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


$remote_addr 访问服务器的机器的IP地址
$remote_user   使用的哪个用户登录的
[$time_local]  访问时间
$request   url
$status   状态码
$body_bytes_sent  nginx发送了多少数据
$http_referer  从哪个网站跳转,引流过来的
$http_user_agent  用的什么浏览器
$http_x_forwarded_for   是否有代理,转发

###########################################  

2.2   日志文件 logs

access.log  -->记录正常的访问
error.log --> 记录错误的访问
nginx.pid--> 记录master进程的pid号

###########################################  

2.3  网页文件 html

 404.html : 出现404错误显示这个页面

50x.html:出现50错误显示这个页面 

index.html: 网页根目录,访问nginx web服务器会默认访问这个页面

###########################################  

3. 虚拟主机

基于IP的虚拟主机   :一个网站对应一个公网IP
基于端口的虚拟主机   : 一个网站对应一个端口

3.1  基于域名的虚拟主机

优点:节省服务器,省钱

缺点:一台虚拟服务器收到攻击,其他的会受到牵连,共用cpu,内存,磁盘,带宽,如果一台服务器的访问量特别大,会导致其他·服务器访问受到影响

###########################################  

3. 2   基于域名的虚拟主机配置

 示例:添加域名 www.liming.com

3.2.1  修改配置文件,添加www.liming.com的配置

在nginx.conf主配置文件里面添加如下配置

 server {
        listen  80;
        server_name www.liming.com;
        access_log  logs/liming.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }

###########################################  

3.2.2  新建相关目录和文件

3.2.3  检测语法,刷新nginx服务

nginx -t 

[root@www html]# nginx -t
nginx: the configuration file /usr/local/scliming99/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/scliming99/conf/nginx.conf test is successful

nginx -s reload  刷新服务

[root@www html]# nginx -s reload

###########################################  

3.2.4   修改hosts文件

linux系统下面:

添加 域名配置 

[root@www html]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.44.130 www.liming.com

然后使用浏览器访问域名

[root@www html]# curl www.liming.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

###########################################  

windows 下面:

hosts目录:

C:\Windows\System32\drivers\etc

 添加域名配置

 使用浏览器访问域名

直接访问 www.liming.com

猜你喜欢

转载自blog.csdn.net/qq_48391148/article/details/124528745