nginx静态配置&安装

Posted xao08

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx静态配置&安装相关的知识,希望对你有一定的参考价值。

systemctl start nginx  #启动
systemctl enable nginx #默认开机自启
systemctl status nginx #查看启动状态
nginx -t #检查配置是否正确
nginx -s reload #重新加载配置
ps -ef|grep nginx #查看进程

1.安装

yum -y install nginx
若这一步报错 证明我们的yum源里没有此安装包 需要配置安装包
cd /etc/yum.repos.d/
vi nginx.repo
写入一下内容
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

2.配置 /etc/nginx/nginx.conf 文件

写入一下内容

user nginx;
worker_processes  8;   ###线程数
worker_cpu_affinity auto;
worker_rlimit_nofile 65535; 
pid        /var/run/nginx.pid;
events 
    use  epoll;
    worker_connections  65535;

http 
    include       mime.types;
    default_type  application/octet-stream;
     log_format mains $server_name "|" $remote_addr "|" [$time_iso8601] "|" "$request"  "|"
$status  "|" $body_bytes_sent "|"  "$http_referer"   "|"
"$http_user_agent"  "|"  $upstream_addr "|" $request_time   "|" $upstream_response_time|;

   client_max_body_size 100M;
   client_body_buffer_size 1024k;
   client_header_buffer_size 32k;
   sendfile        on;
   gzip on;
   tcp_nopush     on;
   keepalive_timeout  300;
   proxy_connect_timeout    300;
   proxy_send_timeout     300;
   proxy_read_timeout     300;
   proxy_ignore_client_abort on;
   gzip_min_length 1k;
   gzip_buffers 4 16k;
   gzip_comp_level 2;
   gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
   gzip_disable "MSIE [1-6]\\."; 
   gzip_vary off;
   include /etc/nginx/conf.d/http/*.conf;    ### 子配置文件
    

==nginx -t 检查配置是否正确==

[root@localhost http]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

5.新建include子配置文件

/etc/nginx/conf.d/http/*.conf

[root@localhost http]# pwd
/etc/nginx/conf.d/http
[root@localhost http]# ll
总用量 4
-rw-r--r--. 1 root root 98 5月  12 00:59 xiao.conf
[root@localhost http]# cat xiao.conf
server  
  listen   80;
  location  / 
     root  /opt/static ;
     index  index.html;
    

[root@localhost http]# 
写入以下内容
server  
  listen   80;
  location  / 
     root  /opt/static ;
     index  index.html;
    

==nginx -t 检查配置是否正确==

6.配置静态页面地址

[root@localhost ~]#  mkdir -p /opt/static
[root@localhost http]# cd /opt/static
[root@localhost static]# ll
total 4
-rw-r--r--. 1 root root 29 Apr 12 13:21 index.html
[root@localhost static]# vi  index.html

检查配置是否正确
nginx -t ###检查语法
nginx -s reload ###重新加载配置
ps -ef|grep nginx ###查看进程

以上是关于nginx静态配置&安装的主要内容,如果未能解决你的问题,请参考以下文章

nginx下discuz! x3 伪静态化的配置

nginx静态配置&安装

nginx静态资源配置(转发)

Nginx 简易教程

若依前端部署Ubuntu安装nginx并配置静态资源

Nginx安装