本文章适合新手,其中含有我在实际中遇到的问题解决分享,服务器在搬瓦工租的,不多废话直接上干货
一、Ubuntu环境:
- 操作系统:Ubuntu 18.04.4
Nginx搭建
1.安装 nginx
sudo apt update
sudo apt install nginx
nginx -v
相关命令
# 启动
service nginx start
# 停止
service nginx stop
# 重起
service nginx restart
sudo systemctl restart nginx
nginx.conf 文件,路径为:/etc/nginx/nginx.conf
常规配置如下:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_vary on;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_min_length 1000;
gzip_proxied any;
gzip_disable "msie6";
#gzip_http_version 1.0;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
server {
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
listen 80;
server_name localhost;
location ~ .*\.(css|js|swf|html|htm|pdf)$ {
root /var/www/html;
autoindex on;
index index.html index.htm;
}
location / {
charset utf-8;
root /var/www/html;
index index.html index.htm;
try_files $uri /index.html;
}
}
}
default 服务主机配置文件,路径为:/etc/nginx/sites-available/default
默认页面index.html路径: /var/www/html
二、CenOS环境:
- 操作系统:CenOS 6 x86
- Nginx 版本:1.14.0
Nginx搭建
1.第一步 创建源配置
在/etc/yum.repos.d/目录下创建一个源配置文件nginx.repo
cd /etc/yum.repos.d/
vim nginx.repo
填写如下内容:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
保存,则会产生一个/etc/yum.repos.d/nginx.repo文件。
2.第二步 安装
直接执行如下指令即可自动安装好Nginx:
yum install nginx -y
安装完成,下面直接就可以启动Nginx了:
/etc/init.d/nginx start
现在Nginx已经启动了,直接访问服务器就能看到Nginx欢迎页面了的。
如果还无法访问:
方法一:查看下服务器上端口是否80(阿里云上配下80端口)。
方法二:需配置一下Linux防火墙,以此执行如下命令:
iptables -I INPUT 5 -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
service iptables save
service iptables restart
Nginx的相关命令:
/etc/init.d/nginx start # 启动Nginx服务
/etc/init.d/nginx stop # 停止Nginx服务
/etc/nginx/nginx.conf # Nginx配置文件位置
chkconfig nginx on #设为开机启动
第三步 前端小优化 nginx开启gzip和缓存配置 (nginx.conf)
进入nginx 配置文件vim /etc/nginx/nginx.conf
,添加如下配置:
# 开启gzip
gzip on;
# 启用gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 1k;
# gzip 压缩级别,1-10,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明
gzip_comp_level 2;
# 进行压缩的文件类型。javascript有多种形式。其中的值可以在 mime.types 文件中找到。
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png font/ttf font/otf image/svg+xml;
# 是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_vary on;
# 禁用IE 6 gzip
gzip_disable "MSIE [1-6]\.";
# 开启缓存
location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
access_log off;
expires 30d;
}
location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
access_log off;
expires 24h;
}
location ~* ^.+\.(html|htm)$ {
expires 1h;
}
location ~* ^.+\.(eot|ttf|otf|woff|svg)$ {
access_log off;
expires max;
}
# 格式
# expires 30s;
# expires 30m;
# expires 2h;
# expires 30d;
相关报错问题处理
1.vim使用报错:-bash: vim: command not found
使用命令 : yum -y install vim*
2.解决Nginx下使用React-router(其他单页应用vue-router路由)刷新出现404问题
server需要重定向到index ,进入配置 vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name zlzkj.io;
index index.html;
root /Volumes/Mac/www/antd-admin/;
location / {
try_files $uri $uri/ /index.html; // 指向index.html
}
}
至此,Nginx已经全部配置安装完成,如有问题欢迎留言或联系。