(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;
# }
#}
}
访问对应静态资源路径,若配置成功则可以在路径下看到提前上传的资料。