Nginx服务器简单入门

xiaoxiao2025-08-31  10

1.Nginx 环境搭建

1.Mac下搭建 Nginx

1.brew 简介

brew又叫Homebrew,是Mac中的一款软件包管理工具,通过brew可以很方便的在Mac中安装软件或者是卸载软件。一般Mac电脑会默认安装有brew, 常用指令如下:

- brew 搜索软件 brew search nginx - brew 安装软件 brew install nginx - brew 卸载软件 brew uninstall nginx - brew 升级 sudo brew update - 查看安装信息(经常用到, 比如查看nginx安装,配置文件,虚拟主机等目录) sudo brew info nginx - 查看已经安装的软件 brew list

2.brew安装nginx

安装nginx 可以用brew很方便地安装nginx. sudo brew install nginx

#####1.启动nginx服务

sudo brew services start nginx

浏览器访问:http://localhost:8080, 如果出现nginx界面,说明启动成功.

1)查看nginx版本 nginx -v 2)查看nginx进程是否启动 lsof -i:80 或者 ps -ef | grep nginx 3)杀死进程 kill -QUIT 进程PID 4)查看端口号是否被占用 netstat -an | grep 8090
2.关闭nginx服务

sudo brew services stop nginx

另外几个比较方便的指令

3.重新加载nginx

nginx -s reload

4.停止nginx

nginx -s stop

3.Nginx 安装路径

1.执行 which nginx 或 brew info nginx 查看 Nginx 路径信息

- 这个是nginx配环境变量 /usr/local/bin/nginx - 查看nginx安装的路径 /usr/local/Cellar/nginx/1.15.1

2.查看 Nginx 配置文件路径

- 查看nginx的配置文件, 默认的端口是8080 /usr/local/etc/nginx/nginx.conf

3.默认的网站目录

/usr/local/var/www

2.Window下搭建Nginx

https://blog.csdn.net/kingscoming/article/details/79042874

https://www.imooc.com/article/15667

3.Linux下搭建Nginx

https://segmentfault.com/a/1190000007116797

http://www.runoob.com/linux/nginx-install-setup.html

2.Nginx 基本配置

http://nginx.org/en/docs/

1.基本配置

http://nginx.org/en/docs/http/ngx_http_core_module.html

# 修改配置文件需要重启服务器 user liujun liu; # 1.Nginx worker 进程运行的用户及用户组 worker_processes 1; # 2.Nginx worker 进程个数 events { worker_connections 1024; #3.每个 worker 的最大连接数 } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 4.keepalive 超时时间 # 5.创建虚拟主机(主机名称为:localhost) server { listen 8090; server_name localhost; location / { # 6.location的值为 / , 代表匹配所有请求 root html; # 7.项目发布的目录, 例如:/usr/local/var/www/webapps/jd index index.html index.htm; # 8.指定首页索引文件的名称 } } } # 注意该配置文件的末尾不能有多余的空行

如下图:

浏览器访问: http://127.0.0.1:8090/

2.修改虚拟主机的名称

# 修改配置文件需要重启服务器 user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 1.创建虚拟主机 server { listen 8090; # 2.主机名称为:www.liujun.com 或者 localhost server_name www.liujun.com localhost; # 3.本地测试要在host添加 www.liujun.com location / { root html; index index.html index.htm; } } }

浏览器访问: http://127.0.0.1:8090/ 或者 http://localhost:8090/ 或者 http://www.liujun.com:8090/

mac 电脑 hosts 文件在 /etc/hosts

3.修改发布的目录

# 修改配置文件需要重启服务器 user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 1.创建虚拟主机(主机名称为:localhost) server { listen 8090; server_name localhost; location / { root /usr/local/var/www/webapps/blog; # 2.修改发布项目的目录 index index.html index.htm; } } }

浏览器访问: http://127.0.0.1:8090/ 或者 http://localhost:8090/

3.Nginx 创建虚拟主机

http://nginx.org/en/docs/http/ngx_http_core_module.html

1.创建一个虚拟主机

# 修改配置文件需要重启服务器 user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 1.创建一个虚拟主机(虚拟主机名称为:www.liujun.com) server { listen 8090; server_name www.liujun.com; # 本地测试要在hosts中添加对 www.liujun.com 的解析 location / { root /usr/local/var/www/webapps/blog; # 修改发布项目的目录 index index.html index.htm; } } } # 注意该配置文件的末尾不能有多余的空行

浏览器访问:http://www.liujun.com:8090/

2.创建2个虚拟主机监听同一个端口

# 修改配置文件需要重启服务器 user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 1.创建第一个虚拟主机(虚拟主机名称为:www.liujun.com) server { listen 8090; server_name www.liujun.com; # 本地测试要在hosts中添加对 www.liujun.com 的解析 location / { root /usr/local/var/www/webapps/blog; # 修改发布项目的目录(blog项目中只有一个index.html) index index.html index.htm; } } # 2.创建第二个虚拟主机(虚拟主机名称为:www.new.liujun.com) server { listen 8090; server_name www.new.liujun.com; # 本地测试要在hosts中添加对 www.liujun.com 的解析 location / { root /usr/local/var/www/webapps/new; # 修改发布项目的目录(new项目中只有一个index.html) index index.html index.htm; } } } # 注意该配置文件的末尾不能有多余的空行

浏览器访问:http://www.liujun.com:8090/ 和 http://www.new.liujun.com:8090/ ,

如下图,已经实现在一台服务器中发布多个网站( 一个端口对应多个域名 )

3.创建多个虚拟主机

# 修改配置文件需要重启服务器 user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 1.创建第一个虚拟主机(虚拟主机名称为:www.liujun.com) server { listen 8090; server_name www.liujun.com; # 本地测试要在hosts中添加对 www.liujun.com 的解析 location / { root /usr/local/var/www/webapps/blog; # 修改发布项目的目录(blog项目中只有一个index.html) index index.html index.htm; } } # 2.创建第二个虚拟主机(虚拟主机名称为:www.new.liujun.com) server { listen 8090; server_name www.new.liujun.com; # 本地测试要在hosts中添加对 www.new.liujun.com 的解析 location / { root /usr/local/var/www/webapps/new; # 修改发布项目的目录(new项目中只有一个index.html) index index.html index.htm; } } # 3.创建第三个虚拟主机(虚拟主机名称为:www.liujun.com) server { listen 9090; server_name www.liujun.com; # 本地测试要在hosts中添加对 www.liujun.com 的解析 location / { root /usr/local/var/www/webapps/music; # (music项目中只有一个index.html) index index.html index.htm; } } } # 注意该配置文件的末尾不能有多余的空行

浏览器访问:http://www.liujun.com:9090/

如下图,已经实现在一台服务器中发布多个网站( 一个域名 对应多个端口 , 一个端口对应多个域名 )

4.配置文件的抽取( 引入外部的配置文件 )

nginx.conf配置文件

# 修改配置文件需要重启服务器 user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 1.创建第一个虚拟主机( 引入外部的配置文件 ) include /usr/local/etc/nginx/nconfig/blog.conf; # 每一个语句的尾部不能少了分号 #include /usr/local/etc/nginx/nconfig/*.conf; # 2.创建第二个虚拟主机( server { listen 8090; server_name www.new.liujun.com; location / { root /usr/local/var/www/webapps/new; index index.html index.htm; } } # 3.创建第三个虚拟主机 server { listen 9090; server_name www.liujun.com; location / { root /usr/local/var/www/webapps/music; index index.html index.htm; } } } # 注意该配置文件的末尾不能有多余的空行

blog.conf 配置文件

# 1.创建第一个虚拟主机(虚拟主机名称为:www.liujun.com) server { listen 8090; server_name www.liujun.com; # 本地测试要在hosts中添加对 www.liujun.com 的解析 location / { root /usr/local/var/www/webapps/blog; # 修改发布项目的目录(blog项目中只有一个index.html) index index.html index.htm; } }

mkdir 新建一个文件夹 ; touch 文件一个文件 ;vim 编辑文件

执行的效果:

转载请注明原文地址: https://www.6miu.com/read-5035524.html

最新回复(0)