当前位置: 首页 > news >正文

oss静态网站托管旅游新闻热点

oss静态网站托管,旅游新闻热点,网站建设方案分析,自己给公司做网站难不难文章目录 一、nginx配置文件修改1.1 配置文件位置1.2 php fastcgi配置1.3 测试 二、nginxphp运行原理三、外网访问内网设置 采用nginxphp作为webserver的架构模式,在现如今运用相当广泛。然而第一步需要实现的是如何让nginx正确的调用php。由于nginx调用php并不是如…

文章目录

  • 一、nginx配置文件修改
    • 1.1 配置文件位置
    • 1.2 php fastcgi配置
    • 1.3 测试
  • 二、nginx+php运行原理
  • 三、外网访问内网设置

采用nginx+php作为webserver的架构模式,在现如今运用相当广泛。然而第一步需要实现的是如何让nginx正确的调用php。由于nginx调用php并不是如同调用一个静态文件那么直接简单,是需要动态执行php脚本。所以涉及到了对nginx.conf文件的配置。

当我在搭建wordpress时,需要搭建一个lnmp环境时,nginx报错:file not found 以及其它错误,我发现,是我的nginx配置错误。

那么本文的主要内容就为大家讲解如何在nginx server中正确配置php调用方法,以及配置的基本原理。

一、nginx配置文件修改

1.1 配置文件位置

Nginx的配置文件默认位置为:/etc/nginx/nginx.conf

在我的环境中 nginx.conf 在 /etc/nginx/nginx.conf

配置文件分析

# nginx运行的用户名
user nginx;
# nginx启动进程,通常设置成和cpu的数量相等,这里为自动
worker_processes auto;# errorlog文件位置
error_log /var/log/nginx/error.log;
# pid文件地址,记录了nginx的pid,方便进程管理
pid /run/nginx.pid;# Load dynamic modules. See /usr/share/nginx/README.dynamic.
# 用来加载其他动态模块的配置
include /usr/share/nginx/modules/*.conf;# 工作模式和连接数上限
events {# 每个worker_processes的最大并发链接数# 并发总数:worker_processes*worker_connectionsworker_connections 1024;
}# 与提供http服务相关的一些配置参数类似的还有mail
http {# 设置日志的格式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记录访问的用户、页面、浏览器、ip和其他的访问信息access_log  /var/log/nginx/access.log  main;# 这部分下面会单独解释# 设置nginx是否使用sendfile函数输出文件sendfile            on;# 数据包最大时发包(使用Nagle算法)tcp_nopush          on;# 立刻发送数据包(禁用Nagle算法)tcp_nodelay         on;# 链接超时时间keepalive_timeout   65;# 这个我也不清楚...types_hash_max_size 2048;# 引入文件扩展名与文件类型映射表include             /etc/nginx/mime.types;# 默认文件类型default_type        application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf;# http服务上支持若干虚拟主机。# 每个虚拟主机一个对应的server配置项# 配置项里面包含该虚拟主机相关的配置。server {# 端口listen       80 default_server;listen       [::]:80 default_server;# 访问的域名server_name  _;# 默认网站根目录(www目录)root         /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;# 默认请求location / {}# 错误页(404)error_page 404 /404.html;location = /40x.html {}# 错误页(50X)error_page 500 502 503 504 /50x.html;location = /50x.html {}}
}

要点说明

  • 1、关于error_log

可以设置log的类型(记录什么级别的信息)有:debug、info、notice、warn、error、crit几种

  • 2、关于sendfile

一般的网络传输过程
硬盘 >> kernel buffer >> user buffer>> kernel socket buffer >>协议栈
使用sendfile后
硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈
可以显著提高传输性能。

  • 3、tcp_nopush和tcp_nodelay

tcp_nopush只有在启用了sendfile时才起作用,
在启用tcp_nopush后,程序接收到了数据包后不会马上发出,而是等待数据包最大时一次性发出,可以缓解网络拥堵。(Nagle化)
相反tcp_nodelay则是立即发出数据包.

1.2 php fastcgi配置

分析完了配置文件后开始配置环境。

因为只是配置PHP的服务器,而且只使用一个端口所以只需要改动server部分

在vim中点击‘i’进入编辑模式

server {listen       80 default_server;listen       [::]:80 default_server;# 这里改动了,也可以写你的域名server_name  127.0.0.1;# 默认网站根目录(www目录)root         /var/www/;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {# 这里改动了 定义首页索引文件的名称index index.php index.html index.htm;}error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}# 这里新加的# PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.# Fastcgi服务器和程序(PHP,Python)沟通的协议.location ~ \.php$ {# 设置监听端口fastcgi_pass   127.0.0.1:9000;# 设置nginx的默认首页文件(上面已经设置过了,可以删除)fastcgi_index  index.php;# 设置脚本文件请求的路径fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;# 引入fastcgi的配置文件include        fastcgi_params;}}

修改完成后将vim编辑器切换到一般一半模式(Esc),然后输入:wq保存退出。

之后重启Nginx服务

service nginx restart

以上就配置成功了,但是上面的配置只是nginx配置部分,更多的内容需要继续学习。

1.3 测试

我们可以通过下面的方法判断Nginx配置是否成功。

在Nginx的网站根目录(/var/www/)下创建一个php文件,随便起名我的是php_info.php

内容如下:

<?php// 顺便可以看一下php的扩展全不全phpinfo();

进入你的网站看看能不能打开文件
你的ip/文件名 例如:127.0.0.1/php_info.php

二、nginx+php运行原理

上边我们已经配置成功了,现在我们来看下具体的原理。

首先简单的讲一讲原理,目前主流的nginx+php的运行原理如下:

  • 1、nginx的worker进程直接管理每一个请求到nginx的网络请求。

  • 2、对于php而言,由于在整个网络请求的过程中php是一个cgi程序的角色,所以采用名为php-fpm的进程管理程序来对这些被请求的php程序进行管理。php-fpm程序也如同nginx一样,需要监听端口,并且有master和worker进程。worker进程直接管理每一个php进程。

  • 3、关于fastcgi:fastcgi是一种进程管理器,管理cgi进程。市面上有多种实现了fastcgi功能的进程管理器,php-fpm就是其中的一种。再提一点,php-fpm作为一种fast-cgi进程管理服务,会监听端口,一般默认监听9000端口,并且是监听本机,也就是只接收来自本机的端口请求,所以我们通常输入命令 netstat -nlpt|grep php-fpm 会得到:
    tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1057/php-fpm
    这里的127.0.0.1:9000 就是监听本机9000端口的意思。

  • 4、关于fastcgi的配置文件,目前fastcgi的配置文件一般放在nginx.conf同级目录下,配置文件形式,一般有两种:fastcgi.conf 和 fastcgi_params。不同的nginx版本会有不同的配置文件,这两个配置文件有一个非常重要的区别:fastcgi_parames文件中缺少下列配置:
    fastcgi_param SCRIPT_FILENAME d o c u m e n t r o o t document_root documentrootfastcgi_script_name;
    我们可以打开fastcgi_parames文件加上上述行,也可以在要使用配置的地方动态添加。使得该配置生效。

  • 5、当需要处理php请求时,nginx的worker进程会将请求移交给php-fpm的worker进程进行处理,也就是最开头所说的nginx调用了php,其实严格得讲是nginx间接调用php。

了解了上面的这五个简单原理,在nginx中配置php调用方法就变得易如反掌。

配置文件详解:

server {  listen       8011;  server_name  test.cn;  location ~ \.php?.*$ {  root           /share/test;  fastcgi_pass   127.0.0.1:9000;  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  include        fastcgi_params;  }  
}  
  • 1、第一个大括号 server{ }:不必多说,代表一个独立的server,
  • 2、listen 8011:代表该server监听8011端口
  • 3、location ~ .php?.*${ }:代表一个能匹配对应uri的location,用于匹配一类uri,并对所匹配的uri请求做自定义的逻辑、配置。这里的location,匹配了所有带.php的uri请求,例如:http://192.168.244.128:8011/test.php/asdasd http://192.168.244.128:8011/index.php等
  • 4、root /share/test:请求资源根目录,告诉匹配到该location下的uri到/share/teset文件夹下去寻找同名资源。
  • 5、fastcgi_pass 127.0.0.1:9000:这行开始是本文的重点:这行代码的意思是,将进入到该location内的uri请求看做是cgi程序,并将请求发送到9000端口,交由php-fpm处理。
  • 6、fastcgi_param SCRIPT_FILENAME d o c u m e n t r o o t document_root documentrootfastcgi_script_name; :这行配置意思是:动态添加了一行fastcgi配置,配置内容为SCRIPT_FILENAME,告知管理进程,cgi脚本名称。由于我的nginx中只有fastcgi_params文件,没有fastcgi.conf文件,所以要使php-fpm知道SCRIPT_FILENAME的具体值,就必须要动态的添加这行配置。
  • 7、include fastcgi_params; 引入fastcgi配置文件
    以上就是最简洁版的nginx启动php脚本的最简配置,当重启nginx之后,在/share/test目录下创建一个xx.php文件,输入<?php echo "hello world"; ?>保存,然后在浏览器中访问localhost:8011/xx.php 就可以在网页上显示hello world了。

三、外网访问内网设置

外网IP:http://58.62.21.107:8382 映射内网服务器IP 192.168.17.56 的 82 端口,即:192.168.17.56:82,需要在nginx.conf 配置文件中开放 82 端口给 外网访问,即下面的配置:

[root@ceshi www]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;
}http {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;tcp_nodelay         on;keepalive_timeout   65;types_hash_max_size 2048;include             /etc/nginx/mime.types;default_type        application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf;server {listen       82 default_server;   # 服务器端口,和外网映射的需保持一致listen       [::]:82 default_server; # 服务器端口,和外网映射的需保持一致server_name  192.168.17.56;root         /data/www/;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {}error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}# PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.# Fastcgi服务器和程序(PHP,Python)沟通的协议.location ~ \.php$ {# 设置监听端口fastcgi_pass   127.0.0.1:9000;# 设置nginx的默认首页文件(上面已经设置过了,可以删除)fastcgi_index  index.php;# 设置脚本文件请求的路径fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;# 引入fastcgi的配置文件include        fastcgi_params;}}# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }}
http://www.hrbkazy.com/news/44715.html

相关文章:

  • 西安免费做网站公司建立网站的流程
  • 一流的品牌网站建设百度官网优化
  • 仿win8 网站网站建网站建设网站
  • 个人网站开发用什么语言网络推广引流是做什么的
  • 做网站用什么软件热点营销案例
  • 做企业网站多少钱小程序开发公司十大排名
  • 爱站网seo查询快速优化排名公司推荐
  • 营销一型网站建设公司阿里云域名
  • 如何判断一个网站是否用织梦建设的五年级上册优化设计答案
  • php做网站时间代码信息发布推广方法
  • 网站短信验证怎么做的东莞网络优化调查公司
  • 网站推送怎么做的关键词优化方法
  • 邯郸推广公司灰色seo关键词排名
  • 怎么增加网站的外链杭州seo整站优化
  • 做网站推广的方法有哪些360优化大师官方下载
  • 网站域名怎么做分录企业网站托管
  • 有名做网站公司淘宝指数查询
  • 博客类网站建设毕业论文网站制作公司官网
  • 网站开发发展趋势2018百度知道小程序
  • 宿州网站推广百度一下你就知道了官网
  • 如何建设风水网站最新的疫情最新消息
  • 比较流行的sns营销网站制作自己的网站
  • 日本 男女做受视频网站今日新闻消息
  • 关于网站建设的知识东莞网络公司代理
  • 企业邮箱腾讯河北seo技术培训
  • 这几年做哪些网站能致富淘宝指数查询入口
  • wordpress可视化编辑器排行百度网站怎么优化排名
  • 网站开发技术 主流怎么进行网络推广
  • 武汉网址模板建站2024年新闻摘抄十条
  • 企业类网站有哪些网络营销做得比较成功的企业