nginx
这是一个使用c发高性能免费开源软件,被用于网络中的负载均衡,端口转发,反向代理等。
我常用它来监听80端口中的http请求,通过判断这些http请求的域名,
ubuntu环境
- 腾讯云 - linux - ubuntu20.04
- nginx版本:1.18.0
下载安装nginx
- 如果想下载压缩文件的话参考本文后面的 Win环境安装nginx
- 直接执行:# sudo apt-get install nginx
- 完成后,nginx的配置文件目录在 /etc/nginx 里面

- 目录结构
- conf.d
- 存放nginx的server配置文件,在这里面可以配置nginx去监听不同端口,对不同域名进行不同的响应操作。
- nginx.conf
- nginx的配置文件
- 里面有对nginx的相关配置。
- 可以在这里面写同conf.d存放的文件相同的东西,因为nginx.conf里面就是会include导入conf.d目录下的配置文件。
- conf.d
编写server配置文件
直接由nginx提供服务
- -
- 一般就是静态资源,如html文件,文件下载等
- 在conf.d目录下
- 新建一个后缀为conf的文件
- 这里以域名www.coolight.cool为例,让nginx监听80端口,如果http请求的域名是www.coolight.cool,就去访问/home/0Acoolgiht/nginx/root目录下的index.html或index.htm文件。
- 文件名 www.coolight.cool.conf
- # vi www.coolight.cool.conf
server {
listen 80; #监听端口
server_name www.coolight.cool; #域名
#-------nginx----------
location / { #链接的后半部分为空时
root /home/0Acoolight/nginx/root; #访问的文件所在目录
index index.html index.htm; #访问的文件名
}
location /hello {
root /home/0Acoolight/nginx/root/hello; #访问的文件所在目录
index index.html index.htm; #访问的文件名
}
}
- -
- 端口80
- 因为http请求如果不写端口,默认就是访问80端口
- 也就是说,访问 http://www.coolight.cool/ 时
- 其实就是 http://www.coolight.cool:80/
- 注意这一行: location / { #链接的后半部分为空时
- 这里的 / 是请求链接中域名后面的部分,这里写 / 就表示当我访问 http://www.coolight.cool/ 时,就去调用/home/0Acoolight/nginx/root/index.html。
- 如果是下面那样:location /hello { ,则是当我访问 http://www.coolight.cool/hello 时,去调用 /home/0Acoolight/nginx/root/hello/index.html。
- 端口80
nginx提供php服务
- -
- -
nginx转发给其他端口
- -
- 这里以tomcat为例,nginx监听tomcat.coolight.cool,然后转发给本地的tomcat(8080端口)
server {
listen 80;
server_name tomcat.coolight.cool;
#-------Tomcat---------
location / {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
- -
- proxy_set_header
- 这个参数是让nginx把一些链接的参数加到这个链接中一起转发过去
- 因为转发后,对于接收方(tomcat),其实是本地的nginx在访问tomcat,这样的话,tomcat会认为访问者(nginx)的IP就是本机127.0.0.1,因此nginx在转发时,需要把真实的IP一起发过去。
- Host
- 这个的作用是把原http请求的Host字段放到转发的请求中去
- 如果不加这个,nginx转发后的请求里就不会有host字段
- 服务器(程序)是靠这个字段来判断你这个请求的域名
- X-Real-IP
- 这个其实意思很明显了,就是请求的真实IP发过去,不然服务器(程序)获取到的IP就是本机127.0.0.1
- X-Forwarded-For
- 表示经过的代理的IP。
- 如果一个 HTTP 请求到达服务器之前,经过了三个代理 Proxy1、Proxy2、Proxy3,IP 分别为 IP1、IP2、IP3,用户真实 IP 为 IP0,那么按照 XFF 标准,服务端最终会收到以下信息:
- X-Forwarded-For: IP0, IP1, IP2
- proxy_set_header
Win环境
- Win11
- nginx版本:1.20.2
下载安装nginx

- 下载后解压zip,并转到解压目录中

- 测试运行
- 双击目录里的nginx.exe程序运行
- 成功启动的话,是没有提示的。
- 然后去浏览器中输入 http://127.0.0.1
- 如果出现nginx的欢迎页面就是成功了。
- 双击目录里的nginx.exe程序运行

- win下nginx的目录结构和ubuntu基本一致,如果没有conf.d目录,就新建一个。
- 然后编辑 conf/nginx.conf 文件
- 找到 http{} 代码段

- -
- 找到这一段的末尾,加上一行:
- include D:/0Acoolight/App/nginx/nginx-1.20.2/conf.d/*.conf;
- 注意include后面的路径需要改成你的nginx的安装目录
- 这一句的意义就是让nginx去导入你放在nginx安装目录下的conf.d文件夹下所有conf后缀的文件。
- 找到这一段的末尾,加上一行:
- 然后在conf.d目录里,就和ubuntu中一样,新建后缀为conf的配置文件,然后在命令行让nginx重新读取配置文件即可。
- 直接在这个目录下右键,新建一个文本文件

- -
- 举例,监听80端口域名为tomcat.coolight.cool的请求,然后转发给tomcat(8080端口)。
- 写入如下内容,注意里面的域名 tomcat.coolight.cool 需要改成你的。
server {
listen 80;
server_name tomcat.coolight.cool;
#-------Tomcat---------
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
- -
- 然后在记事本另存为tomcat.coolight.cool.conf,注意后缀一定是要conf。
- 如果不 另存为 的话,这个文件是以txt结尾的。


- -
- 让nginx重新读取配置文件
- 回到nginx安装目录
- 让nginx重新读取配置文件

- -
- -
- 输入 nginx -s reload 回车执行。
- 如果执行后没有提示就是成功了
- -

- 测试运行
- 在浏览器输入刚刚在配置文件中写的域名
- http://tomcat.coolight.cool
常用命令
- 启动nginx,直接到nginx的安装目录执行:
- ./nginx
- 快速停止nginx
- nginx -s stop
- 完整有序的停止nginx,这个命令会等待所有请求结束后再关闭
- nginx -s quit
- 重新加载配置文件
- nginx -s reload
- 这个需要nginx在运行中,并且执行后nginx也是在运行中。
- 检查配置文件是否有语法错误
- nginx -t
- 检查指定的配置文件是否有语法错误
- nginx -t -c /etc/nginx/conf.d/nginx.conf
常见问题
- Win:'nginx' 不是内部或外部命令,也不是可运行的程序或批处理文件。
- ubuntu:Command 'nginx' not found。
- nginx命令未找到,明明已经安装了。
- 如果你没有配置环境变量的话,需要cd到nginx的安装目录下,才能执行nginx,不然系统会找不到nginx这个命令。

- unknow directive “xxx” in xxx:x
- 配置文件写错了。
- 到提示的文件,和行数中检查。

- nginx: [warn] conflicting server name "xxx" on 0.0.0.0:80, ignored
- 监听服务监听的域名重复了,也就是说,你所有的文件中,server的(域名 + 端口)不能一样,一样的话请写到一起。

- [error] CreateFile() "xxx/nginx.pid" failed (2: The system cannot find the file specified)
- 找不到nginx的pid文件。
- 一般就是nginx没有在运行中,请先启动nginx。

levitra montelukast and fexofenadine hydrochloride uses in hindi The theory is that equity prices can be considered as a discounted price of future dividends buy cialis 5mg online
Klaveniek, G when to take viagra for best results Nonalcoholic liver disease nonalcoholic steatohepatitis, NASH
Testicles Testicular neoplasia is rare in cats when to take viagra Ablation characteristics are summarized in Table 2
propecia walgreens In 1969, a miner who was suffering from a disease known as Black Lung, which leads to death, was suggested by friends to stop working
Since the operation also removes the cord above the testicle, that side of the scrotum can look and feel empty to them viagra with alcohol Gemcitabine generally is accepted as an agent with a favourable toxicity profile 21
During the first few days after birth, the red cells have a normal or high content of GSH 5 mg cialis generic india
how much is viagra at walmart Myth 6 is the politically correct myth, Dr
Cancer has spread to other parts of the body beyond the pelvis, including the abdomen and or lymph nodes in the groin cialis online reviews
The munich doctors have previously 2000, 2003 published studies showing an improved pregnancy rate 43 compared to 20 where 300mcg of molgramostin GM- CSF was given as a single dose injection on the day of ET, but I haven t been able to get a free copy of the full journal articles online finasteride
1990 May 27; 131 21 1147 50 online cialis
Antibiotics are sort of vegan, sort of not vegan, but ultimately something a vegan shouldn t stress about or feel guilty about taking for various reasons is generic viagra safe vivax cases that would have been observed in the prophylactic phase would also have relapsed in the absence of post exposure prophylaxis
Dual agent chemoprotection by retroviral co expression of either MDR1 or MRP1 with the P140K mutant of O6 methylguanine DNA methyl transferase buying cialis online safely I did have really bad acne as a teenager and I had to take accutane twice but my adult acne hasn t been THIS bad
Thus creatinine clearance rate CCR defined as the volume of blood cleared of creatinine per minute closely approximates the GFR and is commonly used as a measure of GFR, which is more difficult to directly measure generic for cialis
Sullivan SM et al cialis 5 mg best price usa 19, 20 A number of trials have directly evaluated the efficacy of OA OS strategies versus CMF cyclophosphamide, methotrexate, and fluorouracil chemotherapy for premenopausal women
Half of the patients in the control group had a BNP drawn on day one, so although they didn t have the multiple BNP protocol, there may not have been much difference in the management of these patients, so we probably shouldn t be too surprised that there were no differences in outcomes zithromax iv
cialis online reviews Kayla, USA 2022 05 30 10 29 49
Heart failure is called congestive heart failure when fluid builds up in various parts of the body cialis from india
Derick TewRYvwpuP 6 20 2022 where to buy cialis Although patients are often jaundiced, they enjoy normal life expectancies
cheapest cialis 20mg 1 307 1 271 1 250 1 200 1 200 1 188 1 187 1 122 1 122 1 122 1 113 1 109 1 056 1 030 1 000 1 000 89 000 10 000 6 000 5 547
After subgroup analysis and sensitivity analysis, we found that Cristofanilli s research may cause heterogeneity 23 viagra nitric oxide Nondestructive determination of lignans and lignan glycosides in sesame seeds by near infrared reflectance spectroscopy
ERОІ1 repressed the mesenchymal spindle shaped morphology of the MDA MB 231 and Hs578T cells and enhanced cell cell contact buy cialis 5mg This would be my first, so I don t know what to expect
In specific population such as cigarette smokers the use of ОІ carotene increases the risk of lung or prostate cancer 205, 206, 207, 212 cialis 20mg I knew about misoprostol before I miscarried specifically that it s not FDA- approved for use on pregnant women, but doctors continue to use it to induce labor, and that it often results in ruptured uteruses
Great online game https://razorsharkplay.com/ where you can win money, buy your loved one a new phone or car, close the mortgage on a room and just two months, hurry up register and win.
[…] 安装/配置教程 […]
@[frp+nginx]实现校园网内网穿透提供http服务(本地windows穿透到Linux云服务器上) | 猫薄荷 But it is what it is buy zithromax online us
@[frp+nginx]实现校园网内网穿透提供http服务(本地windows穿透到Linux云服务器上) | 猫薄荷 cialis coupon I would recommend talking to a physical therapist as they have experience doing this and also they will be able to advise you as to how to protect your own body while doing it
@[frp+nginx]实现校园网内网穿透提供http服务(本地windows穿透到Linux云服务器上) | 猫薄荷 A big hug xoxox can you buy viagra without a prescription An emerging treatment topical ivermectin for papulopustular rosacea
@[frp+nginx]实现校园网内网穿透提供http服务(本地windows穿透到Linux云服务器上) | 猫薄荷 cheapest place to buy cialis Rpl13 was used as the normalization control
@[frp+nginx]实现校园网内网穿透提供http服务(本地windows穿透到Linux云服务器上) | 猫薄荷 While there is no guarantee that a patient will complain about all or any of the following, it is possible for the papilledema patient to also be suffering from blurred vision, transient obscuration, pulsatile tinnitus, diplopia, and visual field defects buy cialis online with a prescription She took 4mg of folate preconception prior to this pregnancy but wants to know if this fetus is affected
[…] 安装/配置教程 […]
@[frp+nginx]实现校园网内网穿透提供http服务(本地windows穿透到Linux云服务器上) – cool薄荷 Budd Chiari syndrome long term effect on outcome with transjugular intrahepatic portosystemic shunt buy cialis non prescription
@[frp+nginx]实现校园网内网穿透提供http服务(本地windows穿透到Linux云服务器上) – cool薄荷 cialis online ERK is a proline directed kinase that preferentially catalyzes the phosphorylation of substrates containing a Pro Xxx Ser Thr Pro sequence Roskoski, 2012
@[frp+nginx]实现校园网内网穿透提供http服务(本地windows穿透到Linux云服务器上) – cool薄荷 comprar levitra sin receta en madrid PubMed Google Scholar Ireland JL, Scheetz D, Jimenez- Krassel F, Themmen AP, Ward F, Lonergan P, Smith GW, Perez GI, Evans AC, Ireland JJ
@[frp+nginx]实现校园网内网穿透提供http服务(本地windows穿透到Linux云服务器上) – cool薄荷 low-dose cialis and viagra together I remember the social worker coming to our home for our first home study visit in the summer of 2009
@[frp+nginx]实现校园网内网穿透提供http服务(本地windows穿透到Linux云服务器上) – cool薄荷 Despite a documented magnesium level of 24 mg dL and significant short term morbidity, the patient completely recovered levitra de 5mg Binding assay for thyrotropin receptor autoantibodies using the recombinant receptor protein