当前位置: 首页 > news >正文

nginx-主配置文件

nginx-主配置文件

  • 一、主配置文件nginx.conf内容
  • 二、修改配置的文件后的操作
  • 三、配置虚拟主机的域名
    • 1. 修改nignx.conf配置文件
    • 2. 新建域名对应的网页根目录
    • 3. 重载nginx配置
    • 4. 验证


一、主配置文件nginx.conf内容

[root@web1 conf]# cat nginx.conf#user  nobody;  # nginx woker进程启动的时候会使用那个用户去启动,默认nobody
worker_processes  2;   # 指定2个woker进程,一般情况下与cpu核心的个数一致
# auto 表示 自动匹配cpu核心数量#error_log  logs/error.log;   #错误日志存放的路径
#error_log  logs/error.log  notice;  # 记录警告及以上级别错误
#error_log  logs/error.log  info;#pid        logs/nginx.pid;   #master进程的pid号#事件块
events {worker_connections  2048;  #定义一个worker进程可以同时并发连接多少个请求     总的请求数=woker进程数*2048use epoll;	# #定义nginx在进行大并发处理的时候采用epoll模型
}# Nginx 支持的 I/O 多路复用模型比较:select、poll 和 epoll
I/O 多路复用解决的大并发连接的时候,nginx如何去处理的问题http {include       mime.types;  # 申明nginx支持的媒体类型,可以传输识别哪些类型的文件default_type  application/octet-stream;  # 默认的发送的数据类型server_tokens off;  # 隐藏nginx的版本号#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';log_format  main  是日志记录格式的名字$remote_addr 是客户机http请求报文里的ip包里的源ip地址$remote_user  远程用户$time_local 访问的时间$request  http请求报文里的url$status 状态码$body_bytes_sent   http响应报文回复的数据大小$http_referer      本次访问是从哪里引流过来的 --》跳转$http_user_agent   客户端的用户代理浏览器$http_x_forwarded_for 记录从那个负载均衡器转发的#access_log  logs/access.log  main;  #访问日志  main是格式的名字sendfile        on;	# # 启用零拷贝传输#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;  #长连接 65秒gzip  on;   # 开启压缩功能,响应报文的body部分进行压缩#一个server对应nginx进程提供的网站服务,一个server对应一个网站  --》虚拟主机 对应一个网站server {listen       80;  # 监听的端口#server_name  localhost;server_name  www.sc.com;   # 网站的域名,默认localhost#charset koi8-r;#access_log  logs/host.access.log  main;   #访问日志#定义路由--》根路径location / {root   html;   # 网页根目录 在html文件夹   html在nginx安装的路径下index  index.html index.htm;    # 定义网站的首页}#error_page  404              /404.html; 	# 出现404状态码,返回/404.html页面(需在网页根目录下)# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html; 设置50x错误指向的页面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;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    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;#    }#}}

二、修改配置的文件后的操作

检测nginx.conf配置文件是否正确

[root@web-2 conf]# nginx -t
nginx: the configuration file /usr/local/nginx1/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx1/conf/nginx.conf test is successful

重载配置

[root@web-2 conf]# nginx -s reload
nginx: [emerg] unknown directive “sanchuang” in /usr/local/nginx1/conf/nginx.conf:4


三、配置虚拟主机的域名

1. 修改nignx.conf配置文件

server {listen       80;server_name  www.feng.com;access_log  logs/feng.com.access.log  main;
error_log  logs/feng.com.error.log;location / {root   html/feng.com;index  index.html index.htm;}error_page  404              /404.html;

增加两个域名则需再写一个server块


2. 新建域名对应的网页根目录

[root@web-2 conf]# cd /usr/local/nginx1/html/
[root@web-2 html]# mkdir feng.com
[root@web-2 html]# cd feng.com/
[root@web-2 feng.com]# vim index.html # 创建一个首页文件
[root@web-2 feng.com]# cat index.html
welcome to feng.com


3. 重载nginx配置

[root@web-2 feng.com]# nginx -t
nginx: the configuration file /usr/local/nginx1/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx1/conf/nginx.conf test is successful
[root@web-2 feng.com]# nginx -s reload


4. 验证

去访问www.feng.com域名对应的网站

另一台linux系统里测试访问
修改linux系统里的/etc/hosts文件,添加www.feng.com对应的ip地址

[root@nginx-1 fd]# vim /etc/hosts
192.168.100.157 www.feng.com

测试在hosts文件里添加的域名是否可以解析到对应的ip地址

[root@nginx-1 fd]# ping www.feng.com
PING www.feng.com (192.168.100.157) 56(84) 比特的数据。
64 比特,来自 www.feng.com (192.168.100.157): icmp_seq=1 ttl=64 时间=0.576 毫秒
64 比特,来自 www.feng.com (192.168.100.157): icmp_seq=2 ttl=64 时间=0.219 毫秒
^C
— www.feng.com ping 统计 —
已发送 2 个包, 已接收 2 个包, 0% packet loss, time 1039ms
rtt min/avg/max/mdev = 0.219/0.397/0.576/0.178 ms

linux访问www.feng.com

[root@nginx-1 fd]# curl www.feng.com
welcome to feng.com

windows系统里测试访问
修改C:\Windows\System32\drivers\etc\hosts文件

使用记事本打开此文件,添加下面的域名解析记录

192.168.100.157 www.feng.com

打开cmd程序,进行测试

C:\Users\Administrator>ping www.feng.com 测试
 
正在 Ping www.feng.com [192.168.100.157] 具有 32 字节的数据:
来自 192.168.100.157 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.100.157 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.100.157 的回复: 字节=32 时间=1ms TTL=64
来自 192.168.100.157 的回复: 字节=32 时间<1ms TTL=64
 
192.168.100.157 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 1ms,平均 = 0ms

用浏览器访问域名,可查看内容

域名访问

用了基于域名的虚拟主机,应该使用域名去访问,如果使用ip访问的话,默认会访问第1个虚拟主机(网站)

http://www.lryc.cn/news/616146.html

相关文章:

  • 异步问题的概念和消除问题技巧
  • 【Tomcat】企业级web应用服务器
  • ATF(TF-A)安全通告 TFV-12(CVE-2024-5660)
  • nestjs官网推荐typeorm而不是prisma的原因
  • 实现MATLAB2024b和M文件关联(防止运行多个MATLAB)
  • 【0基础3ds Max】主工具栏介绍(下)
  • 金融机构在元宇宙中的业务开展与创新路径
  • ATF(TF-A)安全通告 TFV-13(CVE-2024-7881)
  • vue3项目中在一个组件中点击了该组件中的一个按钮,那么如何去触发另一个组件中的事件?
  • RAG (Retrieval-Augmented Generation) 原理详解与实例
  • Stream流应用
  • 工业相机选择规则
  • Java数据结构——LinkedList
  • MariaDB 数据库管理与web服务器
  • JUC学习笔记-----ReentrantLock
  • 通过trae开发你的第一个Chrome扩展插件
  • CST MATLAB 联合仿真超材料开口谐振环单元
  • Pytorch进阶-timm库-00快速开始
  • 数据结构(17)排序(下)
  • Excel常用功能函数
  • 深度相机---双目深度相机
  • CPP多态
  • 信号处理函数中调用printf时,遇到中断为什么容易导致缓冲区损坏?
  • 《广西棒垒球百科》男子垒球世界纪录·垒球2号位
  • 《算法导论》第 16 章 - 贪心算法
  • 【langchain】如何给langchain提issue和提pull request?
  • 机械学习--DBSCAN 算法(附实战案例)
  • 计算机网络:路由聚合是手动还是自动完成的?
  • 亚麻云之数据安家——RDS数据库服务入门
  • 人工智能-python-机器学习-决策树与集成学习:决策树分类与随机森林