【nginx】nginx搭建静态资源服务

本文详细介绍了在CentOS 7系统上安装Nginx的过程,包括必要的依赖库安装、防火墙关闭、目录创建及解压安装等步骤。同时,提供了Nginx配置文件nginx.conf的示例,演示如何配置静态资源路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)安装Nginx

去官网 http://nginx.org/en/download.html下载最新版本Nginx,本人试过在centos8下安装nginx-1.9.13版本会报错,解决一个问题后还会有下个问题,个人不建议使用,可能是因为版本低的问题。在centos7安装nginx-1.9.13版本正常。

在安装nginx前需要干两件重要的事。

#安装编辑工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
#关闭防火墙
[root@localhost ~]# systemctl stop firewalld.service 
 
[root@localhost ~]# systemctl disable firewalld.service 
 
[root@localhost ~]# systemctl status firewalld

 开始安装

 

#在/usr/local路径下创建nginx目录(不创建在安装过程中会自动创建)
[root@localhost local]# mkdir nginx
#将下载好的nginx压缩包放到nginx目录下并解压
[root@localhost nginx]# tar -zxvf nginx-1.17.6.tar.gz
[root@localhost nginx]# ls
nginx-1.17.6  nginx-1.17.6.tar.gz
#进入解压包下执行以下命令
[root@localhost nginx-1.17.6]# ./configure 
[root@localhost nginx-1.17.6]# make
[root@localhost nginx-1.17.6]# make install
#make 与make install结束后会报以下问题,并不影响nginx的成功安装,不用管
make[1]: 离开目录“/usr/local/nginx/nginx-1.17.6”
[root@localhost nginx-1.17.6]# cd ../
[root@localhost nginx]# ls
conf  html  logs  nginx-1.17.6  nginx-1.17.6.tar.gz  sbin
#进入sbin路径下启动nginx
[root@localhost sbin]# ./nginx 
[root@localhost sbin]# ps -ef |grep nginx 
root      57958      1  0 22:30 ?        00:00:00 nginx: master process ./nginx
nobody    57959  57958  0 22:30 ?        00:00:00 nginx: worker process
root      57964   1281  0 22:31 pts/0    00:00:00 grep --color=auto nginx

在浏览器中访问的ngnix安装机器的ip就可以看到nginx的欢迎页了。 

除此之外,再返回/usr/local/nginx路径下,发现原来的文件结构也发生了变化

[root@localhost sbin]# cd ../
[root@localhost nginx]# ls
client_body_temp  html          nginx-1.17.6.tar.gz  scgi_temp
conf              logs          proxy_temp           uwsgi_temp
fastcgi_temp      nginx-1.17.6  sbin

(2)静态资源路径配置nginx.conf文件

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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;

    server {
        listen       8081;
        server_name  192.168.225.100;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        #add by yuhua
        location /images/ {
           # root是将images映射到/static/images/目录下,在该目录下存放图片。
           root  /static/;
           # autoindex on 打开浏览功能
           autoindex on;
        }
        #add by yuhua
        location /files/ {
           root  /static/;
           # autoindex on 打开浏览功能
           autoindex on;
        }
        #add by yuhua
        location /media/ {
           root  /static/;
           # autoindex on 打开浏览功能
           autoindex on;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        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;
    #    }
    #}

}

访问对应静态资源路径,若配置成功则可以在路径下看到提前上传的资料。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小猿架构

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值