教你基于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

相关推荐

联系我们

QQ:951076433

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