nginx基础配置

本文详细介绍了如何通过Homebrew在Windows和Mac系统上安装Nginx,包括验证安装、检查运行状态、配置文件详解、常用命令以及多应用和proxy_pass的配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装

1、通过Homebrew来安装,Windows可以直接到官网下载安装

brew install nginx  

2、验证是否安装完成

nginx -v  

会输出当前安装的版本

nginx version: nginx/1.23.1

3、检查 Nginx 是否正在运行

ps -ef|grep nginx

如果正在运行,会有输出

501 51947     1   0  6:28下午 ??         0:00.00 nginx: master process nginx
501 51948 51947   0  6:28下午 ??         0:00.00 nginx: worker process
501 51961 51938   0  6:28下午 ttys050    0:00.00 grep --color=auto nginx

这里使用 ps 命令列出正在运行的进程。通过将其传送到 grep,可以在输出中搜索特定的单词。上面的示例使用 grep 搜索 nginx。结果显示了三个正在运行的进程,即一个主进程和一个工作进程。如果 Nginx 正在运行,将始终看到一个主进程和一个或多个工作进程。

4、查看nginx安装在哪里

whereis nginx

输出安装的位置

nginx: /opt/homebrew/bin/nginx /opt/homebrew/share/man/man8/nginx.8

开始

常用命令
nginx #启动
nginx -s stop #快速停止
nginx -s quit #停止 停止前完成已接受的请求
nginx -s reload #重新加载
pkill -9 nginx # 强行停止nginx进程

启动nginx后,浏览器访问http:127.0.0.1:8000可以看到nginx欢迎页
在这里插入图片描述

配置

主要分为全局配置、 events配置、http配置、http下的server配置、server下的location配置

# 全局配置
#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配置
events {
    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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65; #超时链接 单位秒

    #gzip  on; #是否开启 gzip 压缩
	
    server {
        listen       8000; # 监听端口
        #listen 127.0.0.1:8000;
		#listen 127.0.0.1;
		#listen 8000;
		#listen *:8000;
		#listen localhost:8000;
		#listen [::]:8000; #ipv6地址
        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;
    #    }
    #}

}
全局配置
#user  nobody; # 定义工作进程使用的user和group身份
worker_processes  1; #运行生成的进程数

#error_log  logs/error.log; # 配置日志 第一个参数定义了存放日志的文件 第二个参数定义日志级别
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid; #定义存储nginx主进程ID的文件

#events配置
events {
    ...
}

# http配置
http{
...
}
events配置
...
#events配置
events {
    worker_connections  1024; # 每个工作进程可以打开的最大并发连接数(和后端服务器建立的连接,还有其他的),而不仅仅是和客户端的连接
    # error_log 同样可以配置error_log字段
    # accept_mutex on; #nginx的多个工作进程将以串行方式接入新连接
    # accept_mutex_delay 500ms; #接入新连接的最大时间间隔
}
...
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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65; #超时链接 单位秒

    #gzip  on; #是否开启 gzip 压缩
	
    server {
        listen       8000; # 监听端口
        #listen 127.0.0.1:8000;
		#listen 127.0.0.1;
		#listen 8000;
		#listen *:8000;
		#listen localhost:8000;
		#listen [::]:8000; #ipv6地址
        server_name  localhost; # 设置虚拟主机的配置
        # server_name example.com www.example.com; # 可以设置多个
		# root html; #为请求设置根目录
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root /data/w3; #此时根目录是 /data/w3
            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;
        #}
    }
    # 可以配置多个 server
	server {
		...
	}
	server {
		...
	}


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

}
location配置

为某个请求URI(路径)建立配置

...
http{
	...
	server {
		...
		location / {
			# proxy_pass  http://127.0.0.1:8082; # 转发
            # index  index.html index.htm; # 定义将要被作为默认页的文件
            # try_files $uri/index.html about.html; #按指定顺序检查文件是否存在,并且使用第一个找到的文件来处理请求
            
		}
		# 可以配置多个location
		location ^/i {
			# alias /data/w3/images/; #定义指定路径的替换路径,这里“/i/top.gif”将由/data/w3/images/top.gif文件来响应。
		}
		location = /download/ {
		    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break; # 改写
		    rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  break;
		    return  403;
		}
	}
}
....

location的匹配是有优先级的

location = / {
    [ configuration A ]
}

location / {
    [ configuration B ]
}

location /documents/ {
    [ configuration C ]
}

location ^~ /images/ {
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ { 
    [ configuration E ] 
} 

请求“/”匹配配置A, 请求“/index.html”匹配配置B, 请求“/documents/document.html”匹配配置C, 请求“/images/1.gif”匹配配置D, 请求“/documents/1.jpg”匹配配置E。

多应用配置

服务器上部署多个应用,比如项目A的访问路径是 /aaa,项目B的访问路径是 /bbb,前端在webpack打包时都会加上publicPath字段

location /aaa {
    alias    /var/www/A; #项目A的真实路径是 /var/www/A
    try_files $uri $url/ /aaa/index.html;
}

location /bbb {
    alias    /var/www/B; #项目A的真实路径是 /var/www/B
    try_files $uri $url/ /bbb/index.html;
}

这样配置好了后,访问不同路径就能对应不同项目了

proxy_pass

使用proxy_pass的时候要注意,结尾有没有/,表达的意思不一样。假设访问 127.0.0.1/test.html

location /test {
    # proxy_pass http://127.0.0.1:3000 # 会转发到 http://127.0.0.1:3000/test/test.html
    # proxy_pass http://127.0.0.1:3000/ # 会转发到 http://127.0.0.1:3000/test.html
}
if

只能在server和location中使用

if (condition) { ... }

计算指定的condition的值。如果为真,执行定义在大括号中的rewrite模块指令,并将if指令中的配置指定给请求。if指令会从上一层配置中继承配置。

条件可以是下列任意一种:

  • 变量名;如果变量值为空或者是以“0”开始的字符串,则条件为假;
  • 使用“=”和“!=”运算符比较变量和字符串;
  • 使用“”(大小写敏感)和“*”(大小写不敏感)运算符匹配变量和正则表达式。正则表达式可以包含匹配组,匹配结果后续可以使用变量$1…$9引用。如果正则表达式中包含字符“}”或者“;”,整个表达式应该被包含在单引号或双引号的引用中。
  • 使用“-f”和“!-f”运算符检查文件是否存在;
  • 使用“-d”和“!-d”运算符检查目录是否存在;
  • 使用“-e”和“!-e”运算符检查文件、目录或符号链接是否存在;
  • 使用“-x”和“!-x”运算符检查可执行文件;

参考教程
https://www.cainiaojc.com/nginx/nginx-index.html
https://tengine.taobao.org/nginx_docs/cn/docs/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值