#nginx简单入门
安装
- 先安装homebrew官网,在终端里执行以下命令
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- 安装nginx
brew install nginx
-
启动nginx
- 打开http://localhost:8080/
Nginx常用命令
# 启动
nginx -s start;
# 重新启动,热启动,修改配置重启不影响线上
nginx -s reload;
# 关闭
nginx -s stop;
# 修改配置后,可以通过下面的命令测试是否有语法错误
nginx -t;
Nginx配置
Nginx及其模块的工作方式由配置文件确定。默认情况下,配置文件名为nginx.conf,放在/usr/local/nginx/conf
/etc/nginx
或者/usr/local/etc/nginx
文件夹中
我的nginx配置文件放在/usr/local/etc/nginx/conf
中,打开配置文件(我使用的是vscode编辑器,code .命令即打开当前目录)
cd /usr/local/etc/nginx
code .
- nginx.conf文件配置项
#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 {
worker_connections 1024;
}
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;
server {
listen 8080;
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;
# }
#}
include servers/*;
}
入门不需要考虑这么多参数,几个必须的就好了,简化一下
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 8000;
server_name localhost;
location / {
root /Users/wuwenjie2001/Documents/personalPro/nginx-study/src/app;
index index.html index.htm;
}
}
#启动进程,通常设置成和CPU的数量相等
worker_process: 1
#工作模式及连接数设置
events {
worker_connections 1024;
}
#http指令 Web 服务器一个重要的功能是服务静态文件(图像或静态HTML页面)。例如,Nginx 可以很方便的让服务器从本读/Users/wuwenjie2001/Documents/personalPro/nginx-study/src/app 获取 html 文件,这只需要在http块指令中的server块指令中设置location块指令。
#在8000端口上监听
listen 8000;
#服务名称
server_name localhost;
#root 返回本地/Users/wuwenjie2001/Documents/personalPro/nginx-study/src/app文件夹中index.html的内容
#index 可访问的文件类型
location / {
root /Users/wuwenjie2001/Documents/personalPro/nginx-study/src/app;
index index.html index.htm;
}
- 接下来就在/Users/wuwenjie2001/Documents/personalPro/nginx-study/src/app文件中创建一个index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
welcome to nginx
</body>
</html>
nginx -s reload
我们再修改一下nginx.conf文件,在http指令块里加上以下代码
http {
http {
upstream WB {
server 10.10.8.173:7000;
}
server {
listen 8000;
server_name localhost;
location / {
# root /Users/wuwenjie2001/Documents/personalPro/nginx-study/src/app;
index index.html index.htm;
proxy_pass http://WB;
}
}
}
}
反向代理【proxy_pass】
所谓反向代理,很简单,其实就是在location这一段配置中的root替换成proxy_pass即可。root说明是静态资源,可以由Nginx进行返回;而proxy_pass说明是动态请求,需要进行转发,比如代理到Tomcat上。(我这里填的是本地7000端口所在的地址)
负载均衡【upstream】
上面的反向代理中,我们通过proxy_pass来指定Tomcat的地址,很显然我们只能指定一台Tomcat地址
再次打开http://localhost:8000/时别忘了reload 可以看到访问8000端口后,返回的内容跟7000端口一样,成功代理到localost:7000
好了,也是初次学习nginx,理解的就这么多了,后序在继续学习