教你基于centos7搭建Nginx网站服务器的方法。

简介

Nginx是一款高性能的HTTP和反向代理服务器,它可以处理高并发请求,具有高负载能力,在本文中,我们将介绍如何在基于CentOS 7系统上搭建Nginx网站服务器

环境准备

1、CentOS 7系统

教你基于centos7搭建Nginx网站服务器的方法。

2、Nginx软件包

3、Web服务器(如Apache)

4、数据库(如MySQL)

5、HTML、CSS、JavaScript等前端技术

6、Git版本控制工具

安装Nginx

1、更新系统软件包

sudo yum update -y

2、安装EPEL源

教你基于centos7搭建Nginx网站服务器的方法。

sudo yum install epel-release -y

3、安装Nginx软件包

sudo yum install nginx -y

4、启动Nginx服务

sudo systemctl start nginx

5、设置Nginx开机自启

sudo systemctl enable nginx

配置Nginx

1、修改Nginx默认配置文件

sudo vi /etc/nginx/conf.d/default.conf

2、在配置文件中找到以下内容:

location / {
    root   html;
    index  index.html index.htm;
}

将其修改为:

location / {
    root   html;
    index  index.html index.htm;
}

3、重启Nginx服务使配置生效

教你基于centos7搭建Nginx网站服务器的方法。

sudo systemctl restart nginx

部署网站目录结构

1、在/usr/share/nginx/html目录下创建一个名为example的文件夹,用于存放网站文件,使用以下命令:

sudo mkdir /usr/share/nginx/html/example

2、将网站文件放入example文件夹中,例如index.html、style.css、script.js等,可以使用以下命令:

sudo vi example/index.html
sudo vi example/style.css
sudo vi example/script.js

3、在Nginx配置文件中添加一个新的location块,指向example文件夹,找到以下内容:

location / {
    ......
}

在其下方添加如下内容:

location ~ .php$ {
    ......
}
location ~ .css$ {
    ......
}
location ~ .js$ {
    ......
}
location ~ .png$ { set_header Content-Type image/png; expires max; add_header Pragma public; };  images and such as png files will be cached for an extended period of time (as set by expires) to reduce server load and allow faster page loads in the future. This also means that if the file is changed outside of your site it will not be served from the cache until the cache expires or the client revalidates the resource with the server (which may take several minutes). The "add_header Pragma public" line makes this resource publicly cacheable by proxies and CDNs, which can help speed up delivery to users in China, for example, where many sites are blocked by firewalls or other restrictions. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-ControlPublic_cache_directives for more information on how to use these headers to control caching behavior for different types of resources. You may need to adjust this depending on your specific use case and target audience for this site. If you're serving images from a database or another external source then you may need to adjust this header as well so that it doesn't return the wrong file or cause problems with caching or loading times.;  images and such as png files will be cached for an extended period of time (as set by expires) to reduce server load and allow faster page loads in the future. This also means that if the file is changed outside of your site it will not be served from the cache until the cache expires or the client revalidates the resource with the server (which may take several minutes). The "add_header Pragma public" line makes this resource publicly cacheable by proxies and CDNs, which can help speed up delivery to users in China, for example, where many sites are blocked by firewalls or other restrictions. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-ControlPublic_cache_directives for more information on how to use these headers to control caching behavior for different types of resources. You may need to adjust this depending on your specific use case and target audience for this site. If you're serving images from a database or another external source then you may need to adjust this header as well so that it doesn't return the wrong file or cause problems with caching or loading times.;  images and such as png files will be cached for an extended period of time (as set by expires) to reduce server load and allow faster page loads in the future. This also means that if the file is changed outside of your site it will not be served from the cache until the cache expires or the client revalidates the resource with the server (which may take several minutes). The "add_header Pragma public" line makes this resource publicly cacheable by proxies and CDNs, which can help speed up delivery to users in China, for example, where many sites are blocked by firewalls或other restrictions。 See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-ControlPublic_cache_directives for more信息如何使用这些标头来控制不同类型资源的缓存行为,您可能需要根据您的特定用例和目标受众对此进行调整,如果您正在从数据库或其他外部来源提供图像,则可能需要调整此标头以避免返回错误的文件或导致缓存或加载时间问题。;  images and such as png files will be cached for an extended period of time (as set by expires) to reduce server load and allow faster page loads in the future,这也意味着如果文件在站点之外被更改,则不会从缓存中提供服务,直到缓存过期或客户端重新验证与服务器相关的资源(这可能需要几分钟)。“add_header Pragma public”行使此资源可通过代理和CDN公开缓存,这有助于加速在中国等地区的用户交付速度,许多站点受到防火墙或其他限制的阻止,有关如何使用这些标头来控制不同类型资源的缓存行为的更多信息,请参阅https://developer,mozilla,org/en-US/docs/Web/HTTP/Headers/Cache-ControlPublic_cache_directives,您可能需要根据您的特定用例和目标受众对此进行调整,如果您正在从数据库或其他外部来源提供图像,则可能需要调整此标头以避免返回错误的文件或导致缓存或加载时间问题。;  images and such as png files will be cached for an extended period of time (as set by expires) to reduce server load and allow faster page loads in the future,这也意味着如果文件在站点之外被更改,则不会从缓存中提供服务,直到缓存过期或客户端重新验证与服务器相关的资源(这可能需要几分钟)。“add_header Pragma public”行使此资源可通过代理和CDN公开缓存,这有助于加速在中国等地区的用户交付速度,许多站点受到防火墙或其他限制的阻止,请参见https://developer,mozilla,org/en-US/docs/Web相关的问题和解答:Q1:如何在CentOS7上安装Nginx?A1:可以通过以下命令安装Nginx:sudo yum install epel-release && sudo yum install nginx,然后启动Nginx服务并设置开机自启:sudo systemctl start nginx && sudo systemctl enable nginxQ2:如何在Nginx中配置反向代理?A2:可以在Nginx配置文件中的server块中添加一个location块,指向后端服务器的地址和端口,`location ^~ /api {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    },其中backend是后端服务器的地址和端口,Q3:如何在Nginx中配置静态文件缓存?A3:可以在Nginx配置文件中的server块中添加一个location块,指向静态文件的路径。location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
        root /path/to/your/staticfiles;
        expires max;
        add_header Pragma public;
    }`,其中root指定了静态文件的路径,expires指定了缓存的时间,add_header Pragma public使得静态文件可以被代理和CDN缓存,Q4:如何在Nginx中配置SSL证书?A4:可以在Nginx配置文件中的server块中添加ssl_certificate和ssl_certificate_key指令,

本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/471707.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
硬件大师硬件大师订阅用户
上一篇 2024年7月9日 11:49
下一篇 2024年7月9日 11:59

相关推荐

  • 我来教你博客主机租用反应速度慢的原因有哪些呢。

    博客主机租用反应速度慢可能是由于多种原因。主机资源不足可能导致CPU被消耗殆尽,内存全部占满,从而影响反应速度。服务器的网络带宽不足也会影响用户访问速度,导致加载缓慢。如果机房网络出现问题,如延迟大、丢…

    2024年7月18日
    03
  • 今日分享如何测试服务器速度。

    使用在线测速工具,如Ping、Speedtest等,输入服务器IP地址进行测试。 在当今的互联网时代,服务器的速度对于网站的性能和用户体验至关重要,了解如何测试服务器速度并优化它是非常重要的,本文将详细介绍如何测试…

    2024年7月11日
    04
  • 说说国内不用备案服务器选择比较好的是哪个。

    国内免备案服务器选择好的有阿里云、腾讯云等。 在国内,选择不需要备案的服务器是一个常见的需求,由于各种原因,有些用户可能不希望或者无法完成备案流程,因此他们更倾向于选择那些不需要备案的服务器,国内不用…

    2024年7月20日
    00
  • 出售域名代理,nginx如何代理域名。

    在网络世界中,域名是网站的唯一标识,而代理服务器则是将客户端的请求转发到目标服务器的一种技术,Nginx是一款高性能的HTTP和反向代理服务器,可以用来代理域名,本文将详细介绍如何使用Nginx代理域名。 我们需要…

    2024年6月30日
    01
  • 我来说说如何在CentOS系统中锁定软件版本阻止升级。

    在CentOS系统中,锁定软件版本阻止升级是一种常见的需求,特别是在企业环境中,为了确保系统的稳定性和安全性,需要对软件版本进行控制,本文将介绍如何在CentOS系统中锁定软件版本阻止升级的方法。 1、使用yum-plu…

    2024年7月8日
    04
  • 说说linux in -s。

    Linux中的in命令用于在文本文件中查找字符串,并将匹配的行打印到屏幕上。-s选项用于指定要搜索的字符串。如果您想在文件“example.txt”中查找字符串“hello”,则可以使用以下命令:cat example.txt | grep 'hello'。…

    2024年7月18日
    01
  • 小编分享ubuntu怎么安装deb文件。

    在Ubuntu 20.04中安装deb文件的方法有很多,这里我们介绍一种简单的方法:使用dpkg命令进行安装,dpkg是Debian软件包管理系统的一部分,它可以用来安装、卸载和管理Debian软件包,下面我们详细介绍如何使用dpkg命令…

    2024年7月6日
    00
  • 我来教你ubuntu重启网络服务命令。

    在Ubuntu系统中,重启网络是非常常见的需求,有时候,你可能需要重启网络服务或者重新配置网络设置,下面是一些方法来帮助你在Ubuntu中重启网络。 重启NetworkManager服务 在Ubuntu中,NetworkManager是一个常用的…

    2024年6月19日
    01

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息