Nginx 汇总

1 minute read

nginx 状态

1.查看

systemctl status nginx

2.启动和重启 nginx 服务

systemctl start nginx
systemctl restart nginx

3.如果有 nxing 的 sbin 目录, 可以直接启动

nginx/sbin/nginx -s reload # 重新载入配置文件
nginx/sbin/nginx -s reopen # 重启 Nginx 并不会重新加载新的配置文件
nginx/sbin/nginx -s stop # 停止 Nginx

4.报错信息 history

nginx emerg invalid number of arguments in “root“ directive -- 解决为配置错误;

5.配置 nginx

server {
    listen 8081;
    server_name localhost ; # charset koi8-r # access_log /var/log/nginx/host.access.log main;

        location / {
        	root /etc/nginx/www/project;
        	index index.html;
        }


        location /api {
        	alias /ect/nginx/www/pro;
        	index index.html;
        	try_files $uri $uri/ /api/index.html
        }

        Location /api/{
          Client_max_body_size 200m;
          Proxy_pass http://192.168.2.101:8000;
          Proxy_redirect off;
          Proxy_set_header Host $host:$service_port;
          Proxy_set_header X-Real-ip $remote_addr;
          Proxy_set_header X-forwarded-For proxy_add_x_forwarded-for;
        }

        error_page 500 502 503 504 /50x.html

        location = /50x.html {
          root /usr/share/nginx/html
        }
}

6.注意: 每次配置的末尾需要加;分号

路径末尾不需要加/斜杠. root 和 alias 区别如下: location /test/ { alias /usr/local/;} location /test/ { root /usr/local/;} alias 找/usr/local/1.jpg 这个文件; root 找/usr/local/test/1.jpg.

7.nginx 配置 systemctl 服务,在/usr/lib/systemd/system/新建 nginx.service 文件

编辑如下内容:

Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

systemctl daemon-reload # 重新载入配置文件 systemctl start nginx #Nginx 启动: systemctl restart nginx #Nginx 重启 systemctl stop nginx #Nginx 停止

8.退出 vim

q!不保存, qw!修改保存文件

9.对于 history 模式(我们的项目) 需要添加如下的格式: try_files $uri $uri/ /index.html; 对于 hash 模式(默认)不用写 try_files;

Tags:

Updated: