nginx 常用命令和配置说明

x33g5p2x  于2021-12-22 转载在 其他  
字(1.8k)|赞(0)|评价(0)|浏览(318)

一 常用命令

1 进入 nginx 目录中

cd /usr/local/nginx/sbin

2 查看 nginx 版本号

./nginx -v

3 启动 nginx

./nginx

[root@iZ25byfwma3Z usr01]# ps -ef | grep nginx

root      1598     1  0 Sep01 ?        00:00:01 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

root      2171  2157  0 14:16 pts/2    00:00:00 grep nginx

usr01     31875  1598  0 09:35 ?        00:00:19 nginx: worker process

4 停止 nginx

./nginx -s stop

5 重新加载 nginx

./nginx -s reload

二 配置文件说明

1 nginx 配置文件位置

cd /usr/local/nginx/conf/nginx.conf

2 配置文件中的内容

包含三部分内容

a 全局块:从配置文件开始到 events 块之间的内容,主要会设置一些影响 nginx 服务器整体运行的配置指令

比如 
worker_processes 1; # 处理并发数的配置

b events 块:影响 Nginx 服务器与用户的网络连接,这部分配置对 nginx 的性能影响较大,在实际中应该灵活配置。

比如 
worker_connections 1024; # 支持的最大连接数为 1024

c http 块:配置最频繁的部分

包含两部分:

  • http 全局块
  • server 块

3 配置文件模板说明

# 全局块worker_processes  1;
# events 块
events {
    worker_connections  1024;
}

# http 块
http {
    # http 全局块
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    # server 块
    server {
        listen       9001;
        server_name  localhost;
        location ~ /pharmacy/ {           
            proxy_pass http://localhost:8201;
        }
        location ~ /cmn/ {           
            proxy_pass http://localhost:8202;
        }
        location ~ /hospital/ {           
            proxy_pass http://localhost:8201;
        }
        location ~ /company/ {           
            proxy_pass http://localhost:8201;
        }
    
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

三 配置文件实战

upstream ofe {
        server 172.17.10.231 weight=2; # 正式环境
        server 172.17.10.235 weight=3; # 正式环境
        # server 172.17.10.231:9020; # 测试环境
        # server 172.17.10.235:9020; # 测试环境
}
# server 块
server {
       listen 80;
       server_name mytest.cn;

       client_max_body_size 1024M;
       sendfile on;
       keepalive_timeout 1800;

       access_log logs/sfe.access.log;
       error_log logs/sfe.error.log;

       location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_connect_timeout 400s;
                proxy_send_timeout 400s;
                proxy_read_timeout 400s;
                proxy_buffering off;
                proxy_pass http://ofe;
                proxy_redirect off;
                }
}

相关文章