CentOS系統rpm安裝Nginx和配置

CentOS rpm安裝Nginx和配置

官方下載地址: http://nginx.org/en/download.html

介紹

Nginx(“engine x”)是一款由俄羅斯的程序設計師Igor Sysoev所開發高性能的 Web和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。

rpm包安裝

#安裝nginx,rpm安裝
#rpm安裝nginx包
rpm -Uvh --force --nodeps nginx-1.16.1-1.el7.ngx.x86_64.rpm

#查看啟動狀態
systemctl status nginx

顯示如下:
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2021-11-26 11:12:41 CST; 5 days ago
     Docs: http://nginx.org/en/docs/
  Process: 1379 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 1543 (nginx)
    Tasks: 5
   CGroup: /system.slice/nginx.service
           ├─1543 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           ├─1544 nginx: worker process
           ├─1546 nginx: worker process
           ├─1547 nginx: worker process
           └─1548 nginx: worker process

11月 26 11:12:41 liang systemd[1]: Starting nginx - high performance web server...
11月 26 11:12:41 liang systemd[1]: Started nginx - high performance web server.

#啟動
systemctl start nginx

#重啟
systemctl restart nginx

#開機自啟動服務
systemctl enable nginx

#查看開機啟動狀態 enabled:開啟, disabled:關閉
systemctl is-enabled nginx

安裝完後在 修改 /etc/nginx/conf.d/default.conf 配置文件,參考內容如下:

vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;


     location /ui {
        alias   /data/dist;
        index  index.html index.htm;
     }
     
     location /file/ {
         root   /home/data/;
        index  index.html index.htm;
     }    
    # websocket配置wss
    location /liangws/ {
        proxy_pass http://192.168.0.19:8080/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Remote_addr $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_read_timeout 600s;
    }

    location ~ /gat {
        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_pass http://localhost:18080 ;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

註意:靜態文件下載,需要依賴nginx,我們需要將這些文件放到 nginx配置文件中的 /home/data/aaa 對應的目錄下。

啟動服務配置

cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

到此這篇關於CentOS系統rpm安裝Nginx和配置的文章就介紹到這瞭,更多相關CentOS rpm安裝Nginx內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: