Nginx入门

1、Nginx简介

Nginx是一个轻量级高性能的HTTP和反向代理的Web服务器。特点:占有内存少、并发能力强。

2、Nginx的安装

Nginx官网

2.1、Windows下

下载对应的版本、下载后直接解压

2.2、Linux下

(1)、安装前准备

#安装依赖:
sudo yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

(2)、下载Nginx包,可官网下载也可利用wget下载

#cd 进入下载包的路径、这里使用的/opt/soft
#在opt下创建soft
mkdir -p /opt/soft
# 进入到soft文件夹
cd /opt/soft

#下载nginx安装包
sudo wget http://nginx.org/download/nginx-1.18.0.tar.gz
# 如果提示sudo: wget:找不到命令,先安装wget
sudo yum install wget
#解压
sudo tar -zxvf nginx-1.18.0.tar.gz

#进入nginx-1.18.0目录
sudo cd nginx-1.18.0
#执行命令并编译
sudo ./configure 
sudo make
sudo make install

2.3、基于docker-compose安装nginx

#docker-compose.yml文件内容
version: '3.1'
services:
  nginx:
    restart: always
    image: nginx
    container_name: nginx
    ports:
      - 80:80
    volumes:
      - ./conf/nginx.conf:/etc/nginx/nginx.conf
      - ./wwwroot:/usr/share/nginx/wwwroot

3、nginx的配置

cd /usr/local/nginx/conf/

默认配置文件说明

#user  nobody;
#启动进程,通常设置成和cpu的数量相等
worker_processes  1;

#工作模式及连接数上限
events {
	#单个后台worker process进程的最大并发链接数
    worker_connections  1024;
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
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;
    
    #指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应     #用,必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘
    #与网络#I/O处理速度,降低系统的uptime.
    sendfile        on;
    #tcp_nopush     on;

    #连接超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #开启gzip压缩
    #gzip  on;

	#服务器相关配置
    server {
    	#侦听80端口
        listen       80;
        
        #定义使用www.xx.com访问
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

 		#默认请求
        location / {
            root   html;
            index  index.html index.htm;
        }

        #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;
    #    }
    #}

4、nginx常用命令

#进入到nginx的sbin目录下
cd /usr/local/nginx/sbin

#命令

#启动
./nginx

#重新加载配置文件
./nginx -s reload

#查看nginx进程
ps aux | grep nginx 查看nginx进程

#停止(强制的)
./nginx -s stop

#安全退出(线程一个个关闭)
./nginx -s quit


5、查看是否安装成功

浏览器输入ip地址,出现以下界面表示安装成功

posted @ 2022-01-15 21:43  胡同咖啡  阅读(43)  评论(0编辑  收藏  举报