Nginx 初次使用
下载
我使用 Arch Linux
1sudo pacman -S nginx
初学者指南
http://nginx.org/en/docs/beginners_guide.html
介绍 + 一些简单操作:
- 打开关闭 Nginx
- 重新加载配置
- 解释配置文件的结构
- 设置 Nginx 为静态内容提供服务
- 将 Nginx 配置为代理服务器(Nginx 本身是代理服务器还是安装并配置好 Nginx 的计算机是代理服务器)
- 将它与 FastCGI 应用程序连接
打开关闭 Nginx,重新加载配置
1sudo nginx # start
2sudo nginx stop # fast shutdown
3sudo nginx quit # graceful shutdown
4
5# if modify the configuration file, need reload nginx
6sudo nginx -s reload # reload
7
8# list all running nginx processes
9ps -ax | grep nginx
配置文件的结构
1name {
2 key: value;
3 # comment
4}
设置 Nginx 为静态内容提供服务
1# edit /etc/nginx/nginx.conf
2http {
3 server {
4 location / {
5 root /usr/share/nginx/html;
6 index index.html index.htm;
7 }
8 error_page 500 502 503 504 /50x.html;
9 location = /50x.html {
10 root /usr/share/nginx/html;
11 }
12 }
13}
将 Nginx 配置为代理服务器
将它与 FastCGI 应用程序连接
1server {
2 location / {
3 fastcgi_pass localhost:9000;
4 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
5 fastcgi_param QUERY_STRING $query_string;
6 }
7
8 location ~ \.(gif|jpg|png)$ {
9 root /data/images;
10 }
11}
使用中的报错
could not build optimal types_hash
1sudo nginx
22021/09/19 08:55:04 [warn] 33400#33400: could not build optimal types_hash, you should increase either types_hash_max_size: 1024 or types_hash_bucket_size: 64; ignoring types_hash_bucket_size
Solution:
1# /etc/nginx/nginx.conf
2http {
3 types_hash_max_size 4096;
4}
Address already in use
1[emerg] 34338#34338: bind() to 0.0.0.0:80 failed (98: Address already in use)
Solution:
1sudo pkill -f nginx & wait $!
2sudo systemctl start nginx
https://wiki.archlinux.org/title/nginx#Configuration_example
https://stackoverflow.com/a/51664874