理解并调通nginx的流程(实战篇)

从default文件构建配置文件,理解并调通nginx的流程

具体的参数配置文档:juejin.cn/post/697172…

我们可以在nginx.conf的文件中发现:其实是对conf.d文件中的配置文件有引用

image-20210707164428564.png

所以我们可以直接在conf.d的文件中新增xxxxx.conf的文件,用来增加nginx的配置文件

我们之前在sites-available和sites-available文件中都发现了default的默认配置文件,可以将这个default复制一份到conf.d的文件夹下,然后重命名为waws.conf文件

操作如下:

  • 拷贝文件到conf.d文件夹下

    • cp sites-available/default conf.d
      复制代码
    • image-20210707164927756.png

  • 将default复制一份叫waws.conf

    • cp default waws.conf
      复制代码
    • image-20210707165254319.png

  • 对waws.conf进行编辑

    • ##
      # You should look at the following URL's in order to grasp a solid understanding
      # of Nginx configuration files in order to fully unleash the power of Nginx.
      # https://www.nginx.com/resources/wiki/start/
      # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
      # https://wiki.debian.org/Nginx/DirectoryStructure
      #
      # In most cases, administrators will remove this file from sites-enabled/ and
      # leave it as reference inside of sites-available where it will continue to be
      # updated by the nginx packaging team.
      #
      # This file will automatically load configuration files provided by other
      # applications, such as Drupal or WordPress. These applications will be made
      # available underneath a path with that package name, such as /drupal8.
      #
      # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
      ##
      
      # Default server configuration
      #
      server {
      	listen 3010 default_server;           # 只是对端口进行下修改
      	listen [::]:3010 default_server;
      
      	# SSL configuration
      	#
      	# listen 443 ssl default_server;
      	# listen [::]:443 ssl default_server;
      	#
      	# Note: You should disable gzip for SSL traffic.
      	# See: https://bugs.debian.org/773332
      	#
      	# Read up on ssl_ciphers to ensure a secure configuration.
      	# See: https://bugs.debian.org/765782
      	#
      	# Self signed certs generated by the ssl-cert package
      	# Don't use them in a production server!
      	#
      	# include snippets/snakeoil.conf;
      
      	root /var/www/html;
      
      	# Add index.php to the list if you are using PHP
      	index index.html index.htm index.nginx-debian.html;
      
      	server_name _;
      
      	location / {
      		# First attempt to serve request as file, then
      		# as directory, then fall back to displaying a 404.
      		try_files $uri $uri/ =404;
      	}
      
      	# pass PHP scripts to FastCGI server
      	#
      	#location ~ \.php$ {
      	#	include snippets/fastcgi-php.conf;
      	#
      	#	# With php-fpm (or other unix sockets):
      	#	fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
      	#	# With php-cgi (or other tcp sockets):
      	#	fastcgi_pass 127.0.0.1:9000;
      	#}
      
      	# deny access to .htaccess files, if Apache's document root
      	# concurs with nginx's one
      	#
      	#location ~ /\.ht {
      	#	deny all;
      	#}
      }
      
      
      # Virtual Host configuration for example.com
      #
      # You can move that to a different file under sites-available/ and symlink that
      # to sites-enabled/ to enable it.
      #
      #server {
      #	listen 80;
      #	listen [::]:80;
      #
      #	server_name example.com;
      #
      #	root /var/www/example.com;
      #	index index.html;
      #
      #	location / {
      #		try_files $uri $uri/ =404;
      #	}
      #}
      复制代码
  • 然后我们保存文件,校验新生成的nginx配置文件的正确性

    • /usr/sbin/nginx -t
      复制代码
    • image-20210707165912860.png

    • 出现上面的successful字段证明配置文件没有问题,报错自行修改配置文件

  • 在重新启动nginx,加载配置文件

    • /usr/sbin/nginx -s reload
      复制代码
    • image-20210707170144867.png

  • 执行完毕后,我们去浏览器看效果

    • image-20210707170240832.png

我们可以看到在default的下面有这样一个配置项的段:我们可以对他进行改造进行,进行复用

server {
    # 监听端口
	listen 80;
	listen [::]:80;

    # 和前面的80端口请求时候,对应多个x.x.x.x:80,可以通过server_name进行区分
	server_name example.com;

    # 访问路径
	root /var/www/example.com;
    # 访问返辉的数据是index.html
	index index.html;

    # 匹配请求路径,执行相对应的操作
	location / {
		try_files $uri $uri/ =404;
	}
}
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享