Linux安装Nginx(yum方式 推荐)

## 摘抄nginx官网文档

URL:http://nginx.org/en/linux_packages.html#stable

1、执行如下命令 创建nginx yum配置文件

cd /etc/yum.repos.d/

touch nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

chmod -R 755 nginx.repo

Replace “OS” with “rhel” or “centos”, depending on the distribution used, and “OSRELEASE” with “6” or “7”, for 6.x or 7.x versions, respectively.

 

2、执行如下命令进行yum安装nginx

yum -y install nginx

 

3、查看nginx版本

# 查看nginx版本
nginx -v

# 查看编译参数
nginx -V

 

4、nginx.conf配置

vi /etc/nginx/nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

   # sendfile        on;
   #tcp_nopush     on;
   # keepalive_timeout  65;
proxy_connect_timeout 600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;
send_timeout 600s;

    #access_log  /var/log/nginx/access.log  main;
client_max_body_size 1024M;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   300;
    types_hash_max_size 2048;

    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

 

5、子配置 示例

vi /etc/nginx/conf.d/test.conf

server {
    listen 80;
    server_name  localhost;
    location / {
      
 # add_header Cache-Control "no-cache, no-store";
 # add_header Access-Control-Allow-Origin *;
 # add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        root /data/web;
        try_files $uri $uri/ /index.html;
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    location /api{
    #proxy_pass http://localhost:8888;
    }
}

 

6、测试nginx配置是否OK

执行 nginx -t

 

7、启动nginx

方式1
cd  /usr/sbin
./nginx

方式2(centos7)
systemctl start nginx

 

8、关闭nginx

方式1:nginx -s stop/quit  
方式2(centos7):systemctl stop nginx

 

9、重新启动nginx

方式1:nginx -s reload
方式2(centos7):sysytemctl restart nginx

 

10、设置nginx开机自启动

vim /etc/sbin/nginx
在文件末尾添加: /usr/sbin/nginx

 

posted @ 2021-12-14 10:35  bug毁灭者  阅读(1394)  评论(0编辑  收藏  举报