nginx入门小册--linux系统

安装

根据系统来安装

$ sudo yum install epel-release && yum install nginx   [On CentOS/RHEL]
$ sudo dnf install nginx                               [On Debian/Ubuntu]
$ sudo apt install nginx                               [On Fedora]

常用命令

  1. 检测配置文件:sudo nginx -t
  2. 启动:sudo service nginx start
  3. 重启:sudo service nginx restart
  4. 查看版本:nginx -v或则nginx -V
  5. 开机自启动:sudo service nginx enable
  6. 查看服务状态:sudo service nginx status
  7. 重新加载配置文件:sudo service nginx reload
  8. 停止服务:sudo service nginx stop
  9. 查看帮助命令:systemctl -h nginx

配置案例

场景一:设置代理

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }

        /***这里是配置内容-start***/
        location /api/ {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For 
            $proxy_add_x_forwarded_for;
            proxy_set_header Host  $http_host;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";
            proxy_pass   http://165.168.0.70:8000;
            proxy_redirect default ;
        }
        /***这里是配置内容-end***/

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

场景二:如果地址访问不到则跳转首页

 server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri /index.html; //这里是配置内容
        }

        location /api/ {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For 
            $proxy_add_x_forwarded_for;
            proxy_set_header Host  $http_host;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";
            proxy_pass   http://165.168.0.70:8000;
            proxy_redirect default ;
        }

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

其他参考

https://www.nginx.cn/doc/

https://juejin.cn/post/6844904000769245191

你可能感兴趣的:(nginx,linux)