## 摘抄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
