当前位置:首页 > 教程 > 正文内容

nginx转发部分路径(nginx转发nginx)

2023-04-25 15:20:05教程1

nginx转发nginx

nginx可以转发内网 url东西。有两种方法:

1、去掉匹配路径转发

匹配规则通常为二级路径,转发的时候过滤掉。

# 转发示例: # 请求:http://somedomain.com/system/path/request # 转发:http://otherdomain.com/path/request # location ^~ /system/ { proxy_pass http://otherdomain.com; }

2、完整路径转发

匹配规则二级路径转发的时候也带上。

# 转发示例 # 请求:http://somedomain.com/system/path/request # 转发:http://otherdomain.com/system/path/request # location ^~ /system { proxy_pass http://otherdomain.com; }

nginx转发502

我常在Nginx服务器上发现502错误,很多情况下并非Nginx本身的问题。就以Nginx+PHP+MySQL这种架构说明。Nginx本身设置等cgi接口返回的数据延时太短,要延长这个时间。

如同前面说的,很多情况下并非Nginx本身的问题,这样操作后常常并不能缓解问题。

此时,就要考虑对应cgi接口的配置,比如 php-fpm.conf 的配置,脚本执行时间的超时情况限制。

这可以通过跟踪php-fpm的 slow log 来排查,对相关代码优化,减少延时。

另外很大的问题在MySQL数据库这一块,如果数据库执行命令超时也会大延长php脚本的执行时间,导致 Nginx 等待超时。

可以my.cnf的 slow log进行确认效能低下的sql语句是哪些,进行优化配置。

通过优化 php-fpm 及 MySQL的配置都大大减少Nginx的等待超时的情况。以上是LNMP的架构为例进行说明。

Nginx转发能抓包吗

一、设备

网线的线序接法,百兆和千兆网线,光纤线,思科路由,华为路由,交换机什么的,包括一些VLAN组建,交换机和路由器之间的学习,一般我建议学习到CCNP就够了,计算机硬件了解,一般你自己DIY过电脑,一般都清楚明白。了解服务器和普通PC的区别,服务器 分别是 1U 2U 4U,然后就是找托管的托管服务器,费用根据机房线路多少,带宽,地域而决定,注册域名,备案最好也懂。

二、数据链路层

关乎就是MAC地址的走向,交换机学习,局域网的东西。

三、网络层

了解中国互联网几大ISP,电信,移动,联通,教育网互相连接的弊端,掌握tcpdump,winshark,的抓包软件,TCP/IP协议,当然也有很多路由器的协议。

四、缓存层

几个开源缓存的软件

squid

varnish

Traffic Server

五、系统层

首先是三大系统,Linux,Unix,Windows,市面上一般就用着三个做服务器系统,Linux 是现今互联网的主流所以一般着力学习这个,Windows是Windows 2008 R2,Unix 就是AIX ,Free BSD,这里并无歧视平台的原因,因为win本身的域控管理也十分强大,但由于win的版权问题,费用高昂,大部分都会侧重于Linux的学习,互联网也倾向这个。AIX和Free BSD这个其实学习了Linux,都差不多,只有些不同,有兴趣的朋友可以去学习一下Linux 和 Uninx之间的历史。

六、数据库层

数据库我自己一般倾向于的Mysql,不过也少有接触Oracle.主要精通一门,估计其它也不难,至于为何选择mysql是因为应用它可以用,深入学习可以了解源码。也有非关系数据库moogoDB

七、容器层

Tomcat,Nginx,Apache,redis,IIS这里没什么好说的了,各自的语言自己选择各种容器来跑应用。

nginx转发真实ip

Nginx 通过配置监听 HTTP 请求,并对其进行转发到 HTTPS 请求来实现将 HTTP 请求转换为 HTTPS 请求。

这个转发过程通常是通过在 Nginx 服务器上配置一个重定向规则来实现的。

该规则告诉 Nginx 将所有通过 HTTP 协议发送的请求重定向到相同的 URL,但使用 HTTPS 协议代替 HTTP。因此,用户在访问网站时将自动使用安全的 HTTPS 协议,而不是明文的 HTTP 协议。

nginx转发去掉前缀

想要搭建个人网站,就需要有单独的服务器,就在阿里云购买了台服务器,选择系统为“Ubuntu 14.04.5 LTS”,并在阿里云买了个域名(域名是为了方便记忆,否则输入ip地址访问网站很不方便),下面就使用Ubuntu系统搭建WordPress个人网站。

安装WordPress运行环境

1.安装Mysql数据库

apt update

apt upgrade

apt install mysql-server

查看mysql是否安装成功:

root@iZ2zeeg42qkecbhciml4pcZ:~# mysql --version

mysql Ver 14.14 Distrib 5.5.62, for debian-linux-gnu (x86_64) using readline 6.3

2.安装PHP

apt-get install software-properties-common

add-apt-repository ppa:ondrej/php

apt update

apt install php7.2

apt install libapache2-mod-php7.2

apt install php7.2-mysql

apt install php7.2-fpm

查看php是否安装成功:

root@iZ2zeeg42qkecbhciml4pcZ:~# php -v

PHP 7.2.16-1+ubuntu14.04.1+deb.sury.org+1 (cli) (built: Mar 7 2019 20:42:24) ( NTS )

Copyright (c) 1997-2018 The PHP Group

Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

with Zend OPcache v7.2.16-1+ubuntu14.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

3.安装Nginx

install nginx

查看Nginx是否安装成功:

root@iZ2zeeg42qkecbhciml4pcZ:~# nginx -v

nginx version: nginx/1.4.6 (Ubuntu)

重启Nginx后,在浏览器中输入http://阿里云服务器外网IP地址/

service nginx stop

service nginx start

如果图片显示为下图,说明阿里云服务器自动启动了apache2的服务,apache2和nginx都使用80端口,80端口冲突。

关闭apache2的服务

重启php7.2-fpm服务和Nginx服务:

在浏览器中输入http://阿里云服务器外网IP地址/

安装WordPress及其配置

1.Mysql创建数据库和用户:

root@iZ2zeeg42qkecbhciml4pcZ:~# mysql -u root -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 44

Server version: 5.5.62-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database 数据库名称 character set utf8 collate utf8_general_ci;

Query OK, 1 row affected (0.00 sec)

mysql> grant all on 数据库名称.* to '用户名'@localhost identified by '用户密码';

Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

mysql> quit

Bye

root@iZ2zeeg42qkecbhciml4pcZ:~#

2.下载WordPress并安装:

获取WordPress软件:点击此处

将下载的wordpress-5.0.3-

zh_CN.tar.gz

上传到云服务器上

安装wordPress:

root@iZ2zeeg42qkecbhciml4pcZ:~# ls

wordpress-5.0.3-zh_CN.tar.gz

root@iZ2zeeg42qkecbhciml4pcZ:~# mv wordpress-5.0.3-zh_CN.tar.gz /var/www/

root@iZ2zeeg42qkecbhciml4pcZ:~# cd /var/www/

root@iZ2zeeg42qkecbhciml4pcZ:/var/www# ls

html wordpress-5.0.3-zh_CN.tar.gz

root@iZ2zeeg42qkecbhciml4pcZ:/var/www# tar -zxvf wordpress-5.0.3-zh_CN.tar.gz

......

root@iZ2zeeg42qkecbhciml4pcZ:/var/www# ls

html wordpress wordpress-5.0.3-zh_CN.tar.gz

root@iZ2zeeg42qkecbhciml4pcZ:/var/www# cd wordpress/

root@iZ2zeeg42qkecbhciml4pcZ:/var/www/wordpress# ls

index.php readme.html wp-admin wp-comments-post.php wp-content wp-includes wp-load.php wp-

mail.php

wp-signup.php xmlrpc.php

license.txt

wp-activate.php wp-blog-header.php wp-config-sample.php wp-cron.php wp-links-opml.php wp-login.php wp-settings.php wp-trackback.php

root@iZ2zeeg42qkecbhciml4pcZ:/var/www/wordpress# mv wp-config-sample.php wp-

config.php

使用vim命令编辑wp-config.php:

vim wp-config.php

修改文件中的数据库配置信息,填写刚才创建的数据库信息:

/** WordPress数据库的名称 */

define('DB_NAME', '数据库名称');

/** MySQL数据库用户名 */

define('DB_USER', '用户名');

/** MySQL数据库密码 */

define('DB_PASSWORD', '数据库密码');

/**

* WordPress数据表前缀。

*

* 如果您有在同一数据库内安装多个WordPress的需求,请为每个WordPress设置

* 不同的数据表前缀。前缀名只能为数字、字母加下划线。

*/

$table_prefix = 'wp_';

在阿里云控制台将域名解析到指定的服务器上:

控制台->域名->解析->添加纪录

配置服务安全组策略,将80(http)端口和443(https)端口开放:

控制台->云服务器ECS->网络和安全->安全组->配置规则

配置80端口:

配置443端口:

配置后查看内容:

编辑Nginx配置文件:/etc/nginx/sites-available/default

client_max_body_size 10m;

server {

listen 80;

listen [::]:80;

server_name localhost; #你的域名

root /var/www/wordpress;

index index.php index.html index.htm index.nginx-debian.html;

location / {

# First attempt to serve request as file, then

# as directory, then fall back to displaying a 404.

#try_files $uri $uri/ =404;

try_files $uri $uri/ /index.php?$args;

}

location ~ \.php$ {

# include fastcgi.conf;

include fastcgi_params;

fastcgi_buffer_size 128k;

fastcgi_buffers 32 32k;

#fastcgi_intercept_errors on;

# With php-fpm (or other unix sockets):

fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

# With php-cgi (or other tcp sockets):

#fastcgi_pass 127.0.0.1:9000;

}

}

重启Nginx后,在浏览器中输入http://阿里云服务器外网IP地址/

service nginx stop

service nginx start

在浏览器中访问自己的域名,查看是否成功:

到此WordPress就安装成功了,你可以自行配制您的网站信息。

nginx转发header配置

第一种:Nginx自己的错误页面

Nginx访问一个静态的html 页面,当这个页面没有的时候,Nginx抛出404,那么如何返回给客户端404呢?

看下面的配置,这种情况下不需要修改任何参数,就能实现这个功能。

server {

listen 80;

server_name www.test.com;

root /var/www/test;

index index.html index.htm;

location / {

}

# 定义错误页面码,如果出现相应的错误页面码,转发到那里。

error_page 404 403 500 502 503 504 /404.html;

# 承接上面的location。

location = /404.html {

# 放错误页面的目录路径。

root /usr/share/nginx/html;

}

}

第二种:反向代理的错误页面

如果后台Tomcat处理报错抛出404,想把这个状态叫Nginx反馈给客户端或者重定向到某个连接,配置如下:

upstream www {

server 192.168.1.201:7777 weight=20 max_fails=2 fail_timeout=30s;

ip_hash;

}

server {

listen 80;

server_name www.test.com;

root /var/www/test;

index index.html index.htm;

location / {

if ($request_uri ~* ‘^/$’) {

rewrite .* http://www.test.com/index.html redirect;

}

# 关键参数:这个变量开启后,我们才能自定义错误页面,当后端返回404,nginx拦截错误定义错误页面

proxy_intercept_errors on;

proxy_pass http://www;

proxy_set_header HOST $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-FOR $proxy_add_x_forwarded_for;

}

error_page 404 /404.html;

location = /404.html {

root /usr/share/nginx/html;

}

}

第三种:Nginx解析php代码的错误页面

如果后端是php解析的,需要加一个变量

在http段中加一个变量

fastcgi_intercept_errors on就可以了。

指定一个错误页面:

error_page 404 /404.html;

location = /404.html {

root /usr/share/nginx/html;

}

指定一个url地址:

error_page 404 /404.html;

error_page 404 = http://www.test.com/error.html;

本网站文章仅供交流学习 ,不作为商用, 版权归属原作者,部分文章推送时未能及时与原作者取得联系,若来源标注错误或侵犯到您的权益烦请告知,我们将立即删除.

本文链接:https://www.xibujisuan.cn/98797838.html