nginx配置, 启动命令, 反向代理配置

2014年1月3日 13:52:07

喜欢这样的风格,干货

http://huoding.com/2013/10/23/290

-----------------下边是我自己的经验(windows)-----------------------

启动nginx

f:/nginxserver/nginx/nginx.exe -c f:/nginxserver/nginx/conf/nginx.conf

在windows下启动nginx要在命令行里进入nginx.exe的那个目录里,否则会提示不能创建日志文件(不能简单的改写环境变量)

nginx: [alert] could not open error log file: CreateFile() "logs/error.log" failed (3: The system cannot find the path specified)

如果域名比较多的话,在http配置块里添加指令:server_names_hash_bucket_size 64;

nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32

如果server 配置块儿中的字符集配置指令如果和默认的冲突了,那就会被忽略掉(我设置为utf8,的冲突了)

1 nginx: [warn] conflicting server name "charset" on 0.0.0.0:80, ignored
2 nginx: [warn] conflicting server name "utf8" on 0.0.0.0:80, ignored

server 块中可以指定日志路径和日志格式,如果指定了日志格式(这里是main),就得保证这个日志格式在http块儿中已经被声明定义了

nginx: [emerg] unknown log format "main" in F:\vc9server\nginx\conf\apache.conf:17

如果全局中已经定义了日志配置指令,则虚拟机中的会被忽略

nginx: [warn] conflicting server name "access_log" on 0.0.0.0:80, ignored

http块儿中的日志格式定义(main为格式名字, 后边的字符串是具体格式)

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

终止nginx

taskkill /F /IM nginx.exe > nul

2019-6-5 10:08:58 星期三

windows 启动脚本:

 1 @echo off
 2 
 3 rem 启动进程前杀掉已有进程
 4 taskkill /f /im nginx.exe
 5 
 6 set currentDir=%cd%
 7 cd %currentDir%
 8 cd nginx
 9 
10 nginx.exe -v
11 echo.
12 echo Start Nginx
13 rem nginx.exe -c ./conf/nginx.conf -p %currentDir%
14 nginx.exe -c ./conf/nginx.conf
15 
16 pause

2019-6-5 9:59:53 星期三

反向代理

 1 ### http段配置
 2     upstream php56 {
 3         #ip_hash;
 4         server 127.0.0.1:8061;
 5         server 127.0.0.1:8062;
 6         server 127.0.0.1:8063;
 7         server 127.0.0.1:8064;
 8         server 127.0.0.1:8065;
 9     
10     }
11     
12     server {
13         listen       80;
14         server_name  www.proxy.com ;
15         location / {
16             proxy_pass_header Server;
17             proxy_set_header Host $http_host;
18             proxy_redirect off;
19             proxy_set_header X-Real-IP $remote_addr;
20             proxy_set_header X-Scheme $scheme;
21             proxy_pass http://php56;
22         }
23     }
24     
25     server {
26         listen       8061;
27         server_name  www.proxy.com;
28         
29         location ~ \.php$ {
30             root           D:\server\code;
31             fastcgi_buffer_size 512k;
32             fastcgi_buffers 32 128k;
33             fastcgi_pass   127.0.0.1:9561;  
34             fastcgi_read_timeout 239;
35             fastcgi_index  index.php;
36             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
37             #fastcgi_param  SCRIPT_FILENAME 
38             include        fastcgi_params;
39         }
40 
41     }
42     
43     server {
44         listen       8062;
45         server_name  www.proxy.com;
46         
47         location ~ \.php$ {
48             root           D:\server\code;
49             fastcgi_buffer_size 512k;
50             fastcgi_buffers 32 128k;
51             fastcgi_pass   127.0.0.1:9562;  
52             fastcgi_read_timeout 239;
53             fastcgi_index  index.php;
54             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
55             #fastcgi_param  SCRIPT_FILENAME  
56             include        fastcgi_params;
57         }
58 
59     }
60     
61     server {
62         listen       8063;
63         server_name  www.proxy.com;
64         
65         location ~ \.php$ {
66             root           D:\server\code;
67             fastcgi_buffer_size 512k;
68             fastcgi_buffers 32 128k;
69             fastcgi_pass   127.0.0.1:9563;  
70             fastcgi_read_timeout 239;
71             fastcgi_index  index.php;
72             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
73             #fastcgi_param  SCRIPT_FILENAME  
74             include        fastcgi_params;
75         }
76 
77     }
78 
79 
80 ###php 测试代码
81 
82 echo '<pre>'; print_r($_SERVER);
83 //先启动多个PHP进程(php-cgi.exe), 监听多个不同的端口(如上的 9561, 9562...), 修改hosts添加 127.0.0.1 www.proxy.com;  然后浏览器访问 wwww.proxy.com, 多次刷新, 观察打印出来的服务端端口的变化