Nginx_入门

  • nginx及其模块的工作方式在配置文件中确定。默认情况下,配置文件名为nginx.conf,并放置在目录/usr/local/nginx/conf、/etc/nginx或/usr/local/etc/nginx中

  • in this case a signal is sent directly to a process with a given process ID. The process ID of the nginx master process is written, by default, to the nginx.pid in the directory /usr/local/nginx/logs or /var/run. For example, if the master process ID is 1628, to send the QUIT signal resulting in nginx’s graceful shutdown, execute:

1
2
kill -s QUIT 1628

  • For getting the list of all running nginx processes, the ps utility may be used, for example, in the following way:
1
ps -ax | grep nginx

[!NOTE]For more information on sending signals to nginx, see Controlling nginx.

1
2
3
#重启前不要忘记用 sudo nginx -t 检查配置是否无错
sudo nginx -s reload #每次修改完配置文件都需要重新启动nginx

[ !NOTE]不推荐使用
sudo systemctl restart nginx 因为这个样子会中断正在运行的服务,上面则反之

配置文件的结构

nginx由由配置文件中指定的指令控制的模块组成。指令分为简单指令和块指令。一个简单的指令由名称和参数组成,用空格分隔,并以分号(;)结尾。块指令的结构与简单指令相同,但它不是分号,而是以一组被大括号({和})包围的附加指令结束。如果块指令可以在大括号内包含其他指令,则称为上下文(示例:事件、http、服务器和位置)。

放置在任何上下文之外的配置文件中的指令都被视为在主上下文中。events和http指令位于main下文、http中的server和server中location中。

#符号后的其余行被视为注释。

主要框架

  • worker_processes
  • event
  • http{mime.types}
  • server{ listen;host_name;location}

location块里的方法名

1
2
3
4
root URL #指定根目录
rewrite DIR URL #重写到URL
return 状态码 URL #重定向或者返回状态码
alias URL

location 正则表达式

1
#还没有公布,未学习到

案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   #gzip  on;

server {
listen 8080;
server_name localhost;

rewrite /url/ /; #会拼接root+/=/usr/share/nginx/html/
#access_log logs/host.access.log main;
location /{
root /usr/share/nginx/html;
index index.html index.htm;
}
location ~ /url/[1-2]{
return 307 /;
}
location = /image{
alias /usr/share/nginx/html/url/image/a.jpg;
default_type image/jpeg;
}
}
#error_page 404 /404.html;


[!WARN]rewrite 的网页最好写绝对路径,重写浏览器只解析相对路径,只看浏览器的URL,也就是定义要重写的路径

beginner