环境Centos7.6 Nginx版本nginx version: nginx/1.20.1 1.直接用yum安装nginx,如果有需要自己编译即可

yum install -y nginx

2.改nginx.conf文件,默认放在etc下的nginx目录,配置文件详解请参考Nginx详解

vim /etc/nginx/nginx.conf
#下面的都是配置文件,可以直接复制替换掉原来的
user nginx;
worker_processes  2;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;
pid        /var/run/nginx.pid;
events {
    use  epoll;
    worker_connections  65535;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
     log_format mains $server_name "|" $remote_addr "|" [$time_iso8601] "|" "$request"  "|"
$status  "|" $body_bytes_sent "|"  "$http_referer"   "|"
"$http_user_agent"  "|"  $upstream_addr "|" $request_time   "|" $upstream_response_time|;
   client_max_body_size 100M;
   client_body_buffer_size 1024k;
   client_header_buffer_size 32k; 
   sendfile        on;
   gzip on;
   tcp_nopush     on;
   keepalive_timeout  300;
   proxy_connect_timeout    300;
   proxy_send_timeout     300;
   proxy_read_timeout     300;
   proxy_ignore_client_abort on;
   gzip_min_length 1k;
   gzip_buffers 4 16k;
   gzip_comp_level 2;
   gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
   gzip_disable "MSIE [1-6]\."; 
   gzip_vary off;
   include /etc/nginx/conf.d/http/*.conf;#这是你静态配置文件的目录文件地址,自己想换啥就换啥
    }

3.修改好了用nginx检查命令看一下啊是否OK

nginx -t
nginx -s reload

4.去include的地址新建一个目录和.conf配置文件

mkdir -p /etc/nginx/conf.d/http
cd /etc/nginx/conf.d/http/
vim www.ceshi.com.conf
#导入以下配置
server {
listen 80; #nginx监听的端口
server_name localhost; #配置网站域名,没有域名就是本机地址
location / {
  root /tmp/site;#静态页面存放路径
  index index.html index.htm;
}
}
mkdir -p /tmp/site
cd /tmp/site
echo hello world > index.html

5.再次用nginx命令检查一下,有异常就看nginx 的日志,把报错放百度

nginx -t
nginx -s reload

启动nginx,设置默认开机自启,查看nginx启动状态

systemctl start nginx
systemctl enable nginx
systemctl status nginx

然后复制你的本机的ip地址到浏览器打开,默认是80端口,我这边静态目录设置了是hello world,有可能是403页面,或者404,具体情况检查一下自己的配置文件和nginx的端口是否被占用,配置文件是否有漏符号。 6.nginx一键配置辅助工具nginx一键配置辅助工具