nginx可以使用yum也可以使用源码包安装,下面以源码包安装为例。
tar -xzvf nginx-1.21.0.tar.gz
cd nginx-1.21.0
     ./configure --help
	    ./configure --prefix=/usr/local/nginx #指定安装路径
我的环境在安装过程中遇到./configure: error: the HTTP gzip module requires the zlib library.问题
yum search zlib搜索依赖包,然后yum install 安装依赖包。
make
make install 如果整个过程没有报错即安装完成
启动前检查nginx默认监听端口80是否被占用
netstat -atlnp|grep :80 或者 lsof -i :80
启动nginx
/usr/local/nginx/sbin/nginx
验证nginx是否启动成功
netstat -atlnp|grep :80 或者 lsof -i :80
登录网页验证nginx是否成功,为了避免windows浏览器缓存对后续实验的影响登录使用elinks在linux进行验证
elinks http://nginxserverip 有下面显示说明nginx安装成功了

nginx默认配置文件中#注释的配置并不是不使用的配置项,而是该配置项默认就是注释行的值
#nginx工作进程使用哪个用户启动,默认使用nobody用户
#user  nobody;
#nginx工作进程的启动数量,一般和服务器cpu数量配置一致
worker_processes  1;
#全局错误日志的位置及日志格式
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid文件位置
#pid        logs/nginx.pid;
events {
   #每个工作进程支持的最大的并发数,线程数。
    worker_connections  1024;
}
#http服务器设置
http {
    #设定mime类型,类型由mime.type文件定义,通常不做修改
    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"‘;
    #$remote_addr与$http_x_forwarded_for用以记录客户端的ip
    #$remote_user客户端名称
    #$time_local访问时间与时区
    #$request请求的url和协议
    #$status请求的状态
    #$body_bytes_sent发送给客户端文件的大小
    #$http_referer从哪个页面连接访问过来的
    #$http_user_agent用户浏览器的信息
    log_format  test  ‘"[test_log]"_$remote_addr - $remote_user [$time_local] "$request" ‘
                      ‘$status $body_bytes_sent "$http_referer" ‘;
  
 #全局访问日志的路径以及使用哪种日志格式
    #access_log  logs/access.log  main;
#零拷贝模式来输出文件,对于普通应用,必须设置为on    
sendfile        on;
#允许或者禁止socket的TCP_CORK选项,在sendfile打开时一并打开,默认状态打开
    #tcp_nopush     on;
    #长链接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #开启压缩,默认打开。可以降低服务器带宽
    #gzip  on;
    #虚拟主机
    server {
       #虚拟机主机监听的端口
        listen       80;
       #虚拟主机的域名
        server_name  localhost;
        #虚拟主机使用的字符集
        #charset koi8-r;
       #该server使用的访问日志存储路径以及使用哪种日志格式
        access_log  logs/test.access.log  test;
        #定义web根路径
        location / {
           #使用nginx下的html作为web的根路径
            root   html;
           #索引页
            index  index.html index.htm;
        }
        location /a {
                allow 182.168.0.0/16;
                allow 127.0.0.1;
                #return https://www.baidu.com;
                deny all;
                #return 502;
                ##触发deny默认返回403,也可以指定本机其它html或者超链接
                #return https://www.jd.com;
        }
        location /b {
                #auth banner
                auth_basic "login auth test";
                #username passwd file
                auth_basic_user_file /etc/my_nginx/htpasswd;
        }
#error_page 404 /404.html;
        # redirect server error pages to the static page /50x.html
        #根据错误码返回对应的错误页
        error_page   500 502 503 504  /50x.html;
       #定义/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;
    #    }
    #}
}
原文:https://www.cnblogs.com/flags-blog/p/14897849.html