nginx 配置

Posted 上了发条的兔子

tags:

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

nginx 配置

1.怎么查看nginx配置有什么错误?

在/usr/local/nginx/sbin目录下

执行nginx -t  查看配置错误信息;

nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (13: Permission denied)
2016/10/24 11:44:09 [warn] 4303#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/nginx/conf/nginx.conf:1
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2016/10/24 11:44:09 [emerg] 4303#0: open() "/var/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

 2.查看nginx的工作进程

ps aux|grep nginx

root 1880 0.0 0.2 33376 5392 ? Ss 10:32 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www 2404 0.0 1.6 57952 30180 ? S 10:37 0:00 nginx: worker process
wenfang 4383 0.0 0.0 112648 976 pts/0 S+ 12:07 0:00 grep --color=auto nginx

worker processes 1;一般设置为cpu数*核数;

3.nginx.conf配置详解

user www www;
worker_processes 2;
  
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
  
pid logs/nginx.pid;
  
  
events {
一般是nginx连接特性
如:有个worker可以允许多少个连接
use epoll;
worker_connections 2048;
}
  
/**这是配置http的主要段; 
http {
include 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 logs/access.log main;
  
sendfile on;
# tcp_nopush on;
  
keepalive_timeout 65;
  
# gzip压缩功能设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
  
# http_proxy 设置
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /usr/local/nginx/proxy_temp 1 2;
  
# 设定负载均衡后台服务器列表
upstream backend {
#ip_hash;
server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
}
  
# 很重要的虚拟主机配置
server {
listen 80;  /*监听80端口
server_name itoatest.example.com;  /*域名
root /apps/oaapp;   /*指向项目目录
  
charset utf-8;
access_log logs/host.access.log main;
  
#对 / 所有做负载均衡+反向代理
location /  {
root /apps/oaapp;
index index.jsp index.html index.htm;
  
proxy_pass http://backend;
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
  
}
  
#静态文件,nginx自己处理,不去backend请求tomcat
location ~* /download/ {
root /apps/oa/fs;
  
}
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /apps/oaapp;
expires 7d;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.10.0/24;
deny all;
}
  
location ~ ^/(WEB-INF)/ {
deny all;
}
#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 html;
}
}
  
## 其它虚拟主机,server 指令开始
}

 

 

 







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

Nginx——Nginx启动报错Job for nginx.service failed because the control process exited with error code(代码片段

Nginx配置文件详细介绍

Nginx 跨域

Nginx的配置

VSCode自定义代码片段11——vue路由的配置

VSCode自定义代码片段11——vue路由的配置